use*_*686 4 angularjs angularjs-directive
有没有办法将配置对象传递给定义为属性指令的自定义指令?
我在Controller中有一个对象要发送到指令:
$scope.configObject = {
count: 5,
mode: 'fast',
getData: myService.getData // function of external service that avaliable in controller
}
Run Code Online (Sandbox Code Playgroud)
在我的视图中我声明了指令:
<div class='list-class' my-list='configObject'></div>
Run Code Online (Sandbox Code Playgroud)
指令看起来像:
return {
restrict: 'A',
link: function(scope, elem, attrs) {
var config = angular.fromJson(attrs.myList);
}
}
Run Code Online (Sandbox Code Playgroud)
我试图使用angular.getJson获取配置对象 - 但它不适用于函数(它可能只获得计数和模式)..getJson()获取配置的方法不正确吗?
另外(我猜它甚至不可能) - 有没有办法让配置对象避免访问
attrs.myList
Run Code Online (Sandbox Code Playgroud)
直?我的意思是如果我改变指令的初始化
.directive('myList', function() { ... }) to
.directive('myCustomList', function() { ... })
Run Code Online (Sandbox Code Playgroud)
我应该改变访问权限
attrs.myCustomList
Run Code Online (Sandbox Code Playgroud)
因为视图看起来像
<div class='list-class' my-custom-list='configObject'></div>
Run Code Online (Sandbox Code Playgroud)
如果需要,可以使用隔离范围传递它
return {
restrict: 'A',
scope: { config : '=myList' }
link: function(scope, elem, attrs) {
//access using scope.config
}
}
Run Code Online (Sandbox Code Playgroud)
或者已经回答你可以从attrs解析它
$parse(attrs["myList"])(scope);
Run Code Online (Sandbox Code Playgroud)
是的,如果您将指令更改为myCustomList,则必须更改代码
scope: { config : '=myCustomList' }
Run Code Online (Sandbox Code Playgroud)
要么
$parse(attrs["myCustomList"])(scope);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3206 次 |
| 最近记录: |