指令链接函数中的“范围”是根范围,而不是指令范围

时间:2015-10-06 11:30:31

标签: javascript angularjs scope directive

我的指令如下

.directive('ticketingChat', function() {
    return {
        restrict: 'E',
        templateUrl:  '/Reply/ReplyScreen', //cshtml from MVC page
        controller: 'TicketingChatController',
        link: function (scope, el, attr) {
            scope.writingTo = "Hey there!";
        }
    }
})

我认为这会创建它自己的范围,而“范围”将允许我访问它。

相反,ng-inspector告诉我:

enter image description here

“范围”实际上是根范围。如何强制指令创建自己的子范围? 如何从链接功能访问该范围?

1 个答案:

答案 0 :(得分:1)

在指令定义对象中设置the invoice email was sent将使AngularJS创建一个继承子范围。将scope: true设置为对象将创建一个独立的子范围。

例如:

scope

有关.directive('ticketingChat', function() { return { restrict: 'E', templateUrl: '/Reply/ReplyScreen', controller: 'TicketingChatController', scope: true, link: function (scope, el, attr) { scope.writingTo = "Hey there!"; // This will be set on the child scope because we have scope: true. } } }) 属性的详细信息,请参阅the docs