简体   繁体   English

在Angular JS中隔离范围

[英]Isolate Scope in Angular js

<form id="form1" runat="server" ng-app="myapp">
    Parent Scope  {{X}}
    <br />

    <div my-customer>
        <div ng-init="X ='Hello World'">
            ISOLATED SCOPE    {{X}}
        </div>
    </div>
</form>

Directive 指示

angular.module('myapp', [])
.directive('myCustomer',function()
{
return {
    restrict: 'A',
    scope: {}

}

}) })

In above code , I have created custom directive with an isolated scope. 在上面的代码中,我创建了具有隔离范围的自定义指令。 Variable X is initiated to 'Hello World' in the Isolated scope of the custom directive.But I am getting below Output 变量X在custom指令的Isolated范围内被初始化为'Hello World'。

Parent Scope Hello World 
ISOLATED SCOPE Hello World

First {{X}} should be empty ( since custom directive has isolated scope) but it has the same value as the second {{X}} . 第一个{{X}}应该为空(因为自定义指令具有隔离的范围),但是它的值与第二个{{X}}相同。 Any idea why? 知道为什么吗? Angularjs version is Angularjs版本是
AngularJS v1.5.3 AngularJS v1.5.3

My first hunch is directive priority. 我的第一个直觉是指令优先级。 From the docs : 文档

priority 优先

When there are multiple directives defined on a single DOM element, sometimes it is necessary to specify the order in which the directives are applied. 当在单个DOM元素上定义了多个指令时,有时有必要指定指令的应用顺序。 The priority is used to sort the directives before their compile functions get called. 优先级用于在调用指令的编译函数之前对它们进行排序。 Priority is defined as a number. 优先级定义为数字。 Directives with greater numerical priority are compiled first. 首先编译具有更高数字优先级的指令。 Pre-link functions are also run in priority order, but post-link functions are run in reverse order. 链接前功能也按优先级顺序运行,但链接后功能则以相反顺序运行。 The order of directives with the same priority is undefined. 具有相同优先级的指令的顺序是不确定的。 The default priority is 0. 默认优先级为0。

and ng-init has a priority of 450 . ng-init的优先级为450 So it would seem ng-init is being run before your scope is isolated by the directive. 因此, 通过指令隔离作用域之前,似乎正在运行ng-init。 Try adding 'priority: 451' to your directive declaration. 尝试在指令声明中添加“ priority:451”。

The scope does simply not get applied at this point. 此刻根本无法应用该范围。

You have to use the template option. 您必须使用模板选项。

 angular.module('myapp', []) .controller('myCtrl', ['$scope', function($scope) { $scope.foobar = "foobar"; }]) .directive('myCustomer', function() { return { restrict: 'EA', scope: { foobar: '@' }, template:"INNER SCOPE {{foobar}}", controller: function($scope) { $scope.foobar = "hello world"; } } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <form id="form1" runat="server" ng-app="myapp" ng-controller="myCtrl"> Parent Scope {{foobar}} <br /> <div my-customer foobar="Hallo welt" /> </form> 

I think the culprit of your problem is ng-init usage. 我认为您问题的根源在于ng-init的用法。 In its documentation doesn't recommend their usage in this way 在其文档中不建议以这种方式使用它们

NgInit works with the scope in pre-link stage with high priority and sets the value into the scope. NgInit在预链接阶段以较高的优先级与范围一起使用,并将值设置为范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM