【问题标题】:How do I pass an object to a directive?如何将对象传递给指令?
【发布时间】:2013-06-19 05:16:17
【问题描述】:

我在获取要传递给我的指令的对象时遇到问题。我相信我做的事情是正确的,但是在一次又一次的尝试失败后,我必须寻求帮助。我在这里错过了什么阻止我将数组传递给我的指令?

HTML:

<div class="body">
   {{orderList.length}} //shows up as 18
</div>
<queue-summary orders="orderList"></queue-summary>

Javascript:

directive('queueSummary', function () {
    return {
        scope: {
            orders: '='
        },
        replace: true,
        restrict: 'E',
        templateUrl: '/partials/admin/bits/queue-summary.htm',
        link: function (scope, element, attrs) {
            console.log(scope, element, attrs); //$attrs.orders show it as the String "orderList" instead of the array
        }
    }
}).

【问题讨论】:

    标签: javascript html angularjs angularjs-directive angularjs-scope


    【解决方案1】:

    值得注意的是,您可以使用 $eval 访问没有隔离作用域的属性的绑定值:

    scope.$eval(attrs.orders)
    

    【讨论】:

      【解决方案2】:

      attrs 只会显示属性的字符串值。要访问传递的对象,请使用您创建的隔离绑定:

      console.log(scope.orders);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-15
        • 1970-01-01
        • 2017-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多