sch*_*cki 4 angularjs angularjs-directive angularjs-scope
我有一个非常奇怪的现象,指令和隔离范围,其中范围中的属性工作或不起作用取决于属性的命名.如果我使用
{check:'@check'}
Run Code Online (Sandbox Code Playgroud)
它工作正常,如预期的那样.但是,如果我使用:
{checkN:'@checkN'}
Run Code Online (Sandbox Code Playgroud)
定义的函数永远不会被分配.一个例子如下:
HTML:
<item ng-repeat="list_item in model.list" model="list_item" checkN="checkName()" check="checkName()" position="$index"></item>'
Run Code Online (Sandbox Code Playgroud)
使用Javascript
app.directive('item', function(){
return {
restrict: 'E',
replace : false,
scope:{
$index: '=position',
check: '&check',
checkN: '&checkN',
model:'='
},
template: '',
link: function(scope, element, attrs){
console.log(scope.check())
console.log(scope.checkN())
}
}
});
Run Code Online (Sandbox Code Playgroud)
然后控制台会给我以下内容:
The checkName function has been called [which is the return string of the function]
undefined
Run Code Online (Sandbox Code Playgroud)
它有可能取决于大写字母的用法吗?这将是非常"意外"的行为.
谢谢你的帮助
schacki
HTML是不区分大小写因此,myAttribute和myattribute将彼此取决于浏览器区分.Angularjs的作者做出了一个关于从html传递到javascript的设计决策,反之亦然.
ngRepeat指令将ng-repeat在视图(html)中使用.同样,您的指令checkN应该用作check-nangular以将其识别为指令.