将范围传递给角度js中的指令

时间:2017-01-17 14:17:33

标签: angularjs angularjs-directive angularjs-scope

我有以下Angular控制器,其中有一个简单的http get:

angular.module('SCtrl').directive('listDirective',function(){
    return{
        restrict:'E',
        scope:{
            listTitle:"@",
            notifications:"="
        },
        templateUrl: '../templates/listWidget.html',
        link: function(scope, element, attrs){
            $scope.notifications=scope.notifications;
        }
    }
});

这很好用。

然后我有以下指令。我希望能够将{{notifications}}放在模块体中,但它不起作用。我猜这是因为我给了指令自己的范围。链接范围的正确方法是什么?我的尝试在下面,但这不起作用。

谢谢!

{{1}}

2 个答案:

答案 0 :(得分:2)

最初误解,正如@devqon指出的那样,您需要传递notification中的list-directive对象,您可以在listWidget.html

中访问该对象
<list-directive notifications="notifications"></list-directive>

答案 1 :(得分:0)

<list-directive notifications="notifications"></list-directive>

link: function(scope, element, attrs){
            scope.notifications=scope.notifications;
        }

您应该分配值notifications =&#34; notifications&#34;否则你无法得到。否则,您可以在指令本身中触发API调用。现在,您不需要分配通知=&#34;通知&#34;。

相关问题