检查angular中链接函数指令中孤立作用域变量的值
check value of isolated scope variable in link function - directives in angular
我有以下html:
<div class="jumbotron" ng-controller="protocolCtrl as pctrl">
<!--IN THIS MODAL YOU CAN ADD/CHANGE/DELETE DATA-->
<modal-directive list="pctrl" headers="['ID', 'Protocol']"></modal-directive>
</div>
在我的modal-directive.html
中,在body中,我这样做:
<!-- Modal body-->
<div class="modal-body">
<table-directive list=list headers=headers></table-directive>
</div>
我想检查我传入的list
参数。如果它等于某个值,我想在body
我的指令是这样的
.directive('modalDirective', function(){
return {
restrict: 'E',
templateUrl: '/directives/modal-directive.html',
scope: {
list: '=',
headers: '='
},
link: function(scope, element, attrs){
if(scope.list == 'pctrl'){
element.find('.modal-body').append('This is just a test.')
}
}
};
});
但是这并没有附加任何东西。如果我去掉if
检查,它就会很好地追加。
我对angular相当陌生,所以如果有人能告诉我如何才能做到这一点,我将不胜感激。
编辑
这就是我如何循环通过我的table-directive.html
<tr ng-repeat="l in list.list">
<!--Access the actual values inside each of the objects in the array-->
<td ng-repeat="data in l"> {{ data }} </td>
<td>
<button type="button" class="btn btn-primary btn-sm"
data-toggle="modal">Edit</button>
</td>
<td>
<button type="button" class="btn btn-danger btn-sm" ng-click="list.removeData(l)"
data-dismiss="modal">Remove</button>
</td>
</tr>
如果你把
<modal-directive list="pctrl" headers="['ID', 'Protocol']"></modal-directive>
和
....
scope: {
list: '=',
headers: '='
},
.....
list: '='
检查元素的list
attr并将参数作为表达式执行,而不是作为字符串我认为您试图将'pctrl'
作为字符串而不是作为范围变量值,以便更改为list="'pctrl'"
作为字符串传递
<modal-directive list="'pctrl'" headers="['ID', 'Protocol']"></modal-directive>
或
使用@
....
scope: {
list: '@',
headers: '='
},
.....
这是一个很好的解释。
这里是angular的官方DOC
如果您只需要检查attr的字符串值,那么您只需使用attrs.list
所以在指令中使用
if(attrs.list === 'pctrl'){
element.find('.modal-body').append('This is just a test.')
}
相关文章:
- 当我更新另一个作用域变量时,作用域变量会自动更新
- 如何在隔离作用域指令中访问此作用域变量
- 正在向构造函数添加作用域变量
- 如何在angularjs中将多个作用域变量传递到自定义指令中
- 作用域变量未从状态父控制器继承到子控制器
- 插槽:访问作用域变量
- 在另一个作用域变量中使用AngularJS作用域变量
- 在Angular.js中通过检查作用域变量进行过滤
- 如何更新作为属性传入的作用域变量
- Node.js中的垃圾收集作用域-变量将保持设置状态多长时间
- 从$http.get调用更新Angular作用域变量
- AngularJS ui路由器:访问子作用域变量
- 访问AngularJS中函数中的作用域变量
- 为什么Angular 1.5双向绑定在将作用域变量传递给组件绑定时失败
- 修改作用域变量后,未更新作用域绑定
- 控制器作用域变量在刷新之前未加载
- 将Angular作用域变量的字符串描述传递给指令
- 将内部作用域变量绑定到外部“模板”
- 无法在 $.post 回调中设置作用域变量
- 如何从在 JS 中用作参数的匿名函数中分配外部作用域变量