angularJS 中的 < 和 @ 作用域隔离有什么区别?
What's the difference between < and @ scope isolation in angularJS?
我的 Rails 应用有不同的组织结构,他们的个人资料页面有自己的库存统计信息 table。
下面写的是app/views/organization/show.html.erb
的部分
<div ng-app="cc-app" class="card-body overflow-hidden">
//codes here
<inventory-stats orgid="<%= @organization.id %>"> </inventory-stats>
//codes here
</div>
我已制定指令以显示库存统计信息并传递 orgid,即具有范围隔离(单向绑定)的组织 ID 以获取组织的库存统计信息。据我所知,我有两种方法可以在 app/assets/javascripts/angular/directives/inventoryStats.directive.js 中为单向绑定定义作用域对象,它们是:
两者都
scope: {
orgid: '@'
}
或者,
scope: {
orgid: '<'
}
但只有@ 似乎有效。对于 @,$scope.orgid 给出指令范围内组织 ID 的值。如果我通过 < 未定义的 orgid。我已阅读 directive's scope documentation 但仍然不知道为什么 < 不起作用。
我的AngularJS:
AngularJS v1.6.10
angular.module('app', [])
.directive('myDirective', function() {
return {
scope: {
param1: '@',
param2: '<'
},
template: '<div><pre>param1= {{param1|json}}</pre><pre>param2= {{param2|json}}</pre></div>'
};
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.js"></script>
<div ng-app="app" ng-init="scopeValue=[1,2,3]">
<my-directive param1="string-value" param2="scopeValue"></my-directive>
</div>
@
仅用于将插值作为 字符串 值传递。
<
是父子数据绑定的一种方式,您可以通过它传递任何您想要的数据值。
我的 Rails 应用有不同的组织结构,他们的个人资料页面有自己的库存统计信息 table。
下面写的是app/views/organization/show.html.erb
的部分<div ng-app="cc-app" class="card-body overflow-hidden">
//codes here
<inventory-stats orgid="<%= @organization.id %>"> </inventory-stats>
//codes here
</div>
我已制定指令以显示库存统计信息并传递 orgid,即具有范围隔离(单向绑定)的组织 ID 以获取组织的库存统计信息。据我所知,我有两种方法可以在 app/assets/javascripts/angular/directives/inventoryStats.directive.js 中为单向绑定定义作用域对象,它们是:
两者都
scope: {
orgid: '@'
}
或者,
scope: {
orgid: '<'
}
但只有@ 似乎有效。对于 @,$scope.orgid 给出指令范围内组织 ID 的值。如果我通过 < 未定义的 orgid。我已阅读 directive's scope documentation 但仍然不知道为什么 < 不起作用。
我的AngularJS: AngularJS v1.6.10
angular.module('app', [])
.directive('myDirective', function() {
return {
scope: {
param1: '@',
param2: '<'
},
template: '<div><pre>param1= {{param1|json}}</pre><pre>param2= {{param2|json}}</pre></div>'
};
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.10/angular.js"></script>
<div ng-app="app" ng-init="scopeValue=[1,2,3]">
<my-directive param1="string-value" param2="scopeValue"></my-directive>
</div>
@
仅用于将插值作为 字符串 值传递。
<
是父子数据绑定的一种方式,您可以通过它传递任何您想要的数据值。