"Scope" in directive linking function is the root scope, not directive scope

"Scope" in directive linking function is the root scope, not directive scope

我有一个指令如下

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

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

相反,ng-inspector 向我展示了:

"Scope" 实际上是根作用域。我如何强制指令创建它自己的子作用域? 我如何从链接函数访问该范围?

在指令定义对象中设置 scope: true 将使 AngularJS 创建一个继承的子作用域。将 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.
        }
    }
})

有关 scope 属性的更多详细信息,请参阅 the docs