Angularjs 指令属性值未传递
大家好,我正在调用指令并将硬代码传递给指令,但它没有传递。我不知道我的代码有什么问题。 代码: 我给指令打电话是这样的:Angularjs 指令属性值未传递,angularjs,Angularjs,大家好,我正在调用指令并将硬代码传递给指令,但它没有传递。我不知道我的代码有什么问题。 代码: 我给指令打电话是这样的: <div ng-controller="EmployeeController"> <off-balance user-key="18" collapse-hide="true"></off-balance> </div> userkey和collapsehide都未定义。请帮助我修复此问题删除scope:true并将c
<div ng-controller="EmployeeController">
<off-balance user-key="18" collapse-hide="true"></off-balance>
</div>
userkey和collapsehide都未定义。请帮助我修复此问题删除
scope:true
并将collapsehide:'=?'
替换为collapsehide:'=?'
idem在您的观察者中删除scope:true
并将collapsehide:'=?'
替换为collapsehide:'=?'请在您的观察者中查找可用的plunker下:
以下是需要注意的要点:
范围
在指令中有两次
collapsehide应该是collapsehide,因为它对属性和指令的命名非常严格
返回指令
应该在函数OffBalance()中
请在下面找到可用的plunker:
以下是需要注意的要点:
范围
在指令中有两次
collapsehide应该是collapsehide,因为它对属性和指令的命名非常严格
返回指令
应该在函数OffBalance()中
我们有什么办法可以同时使用这两个范围吗?不,你不能。这两个范围都有自己的含义。当我们写scope:true
指令得到新的作用域,当我们写scope:{}
指令得到新的隔离作用域时,有什么方法可以同时使用这两个作用域吗?不,你不能。这两个范围都有自己的含义。当我们编写scope:true
指令时,会得到新的作用域;当我们编写scope:{}
指令时,会得到新的隔离作用域。
App.directive('offBalance', ['OffService', 'toaster', OffBalance]);
function OffBalance(OffService, toaster, ) {
var directive = {
link: link,
restrict: 'E',
templateUrl: 'app/modules/sometemplate.html',
scope: {
userKey: '=',
collapsehide: '=?',
},
scope: true
}
}
return directive;
function link(scope, element, attr) {
scope.$watch('userKey', function (newVal) {
alert("userKey" + newVal);
});
scope.$watch('collapsehide', function (newVal) {
alert("collapsehide" + newVal);
});
}
}
var App = angular.module('app', []);
App.directive('offBalance', [OffBalance]);
function OffBalance() {
var directive = {
link: link,
restrict: 'E',
scope: {
userKey: '=',
collapseHide: '=?'
}
}
return directive;
}
function link(scope, element, attr) {
scope.$watch('userKey', function(newVal) {
alert("userKey" + newVal);
});
console.log(scope);
scope.$watch('collapseHide', function(collapseHide) {
alert("collapsehide" + collapseHide);
});
}