javascript - 从字符串变量设置 ng-click 属性值

标签 javascript angularjs angularjs-1.6

我有一个外部 JSON 文件,它是这样的按钮/操作的集合

   {  
   "foo1":{  
      "name":"productDetails",
      "displayAs":"button",
      "action":"viewDetails()"
   },
   "foo2":{  
      "name":"sell",
      "displayAs":"button",
      "action":"sellProduct()"
   }
}

后来,在我看来,我创建了一个 <div>对该 JSON 中包含的每个对象使用 ng-repeat。

我的问题是,我可以像这样在 JSON 文件中设置 ng-click 属性吗?

<li ng-repeat = "field in fields">
    <div ng-click = field.action > {{field.name}} </div>
</li>

最佳答案

您可以使用 ng-bind-html 来做到这一点。

 <li ng-repeat = "field in fields">
    <div bind-html-compile ng-bind-html="trust(field)"  >  </div>
  </li>

添加 bind-html-compile 指令来编译 DOM,以便更改会影响 DOM 元素

指令

 .directive('bindHtmlCompile', ['$compile', function($compile) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            scope.$watch(function() {
                return scope.$eval(attrs.bindHtmlCompile);
            }, function(value) {
                element.html(value);
                $compile(element.contents())(scope);
            });
        }
    };
 }]);

trust 函数中将对象作为参数传递并返回 html 信任元素

 $scope.trust = function(html) {
    return $sce.trustAsHtml('<div ng-click = "' + html.action + '" > {{field.name}} </div>');
 }

演示

angular.module("app",[])
.controller("ctrl",function($scope,$sce){

$scope.viewDetails = function(){console.log("viewDetails")}
$scope.sellProduct = function(){console.log("sellProduct")}
$scope.fields =    {  
   "foo1":{  
      "name":"productDetails",
      "displayAs":"button",
      "action":"viewDetails()" 
   },
   "foo2":{  
      "name":"sell",
      "displayAs":"button", 
      "action":"sellProduct()"
   }
}

$scope.trust = function(html){

return $sce.trustAsHtml('<div ng-click = "'+html.action+'" > {{field.name}} </div>');

}


}).directive('bindHtmlCompile', ['$compile', function ($compile) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                scope.$watch(function () {
                    return scope.$eval(attrs.bindHtmlCompile);
                }, function (value) {
                    element.html(value);
                    $compile(element.contents())(scope);
                });
            }
        };
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <li ng-repeat = "field in fields">
    <div bind-html-compile ng-bind-html="trust(field)"  >  </div>
</li>
</div>

关于javascript - 从字符串变量设置 ng-click 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42978381/

相关文章:

javascript - 以 AngularJS 方式在主页中包含所有 js 文件和 css 文件好吗?

javascript - ReactJs : How to have a default value selected in the RegionDropdown

javascript - 检测元素的特定 CSS 动画的结束

php - 几秒钟后 Laravel 自动注销?

javascript - Angular ui-router - 如何检测到某个状态的直接导航

angularjs - ng-submit 在表单提交后给出旧值

javascript - 在 req.body.id 中未定义(body-parser 和express 4)Nodejs

php - 如何在按下按钮时执行脚本(并最终转移到另一个 URL)?

javascript - 测试 Angular Controller 定义如 angular.module ('myApp' ).controller(

javascript - AngularJS 1.6 中兄弟组件之间的更改通信