AngularJS:为什么我不能将 DOM 属性值传递给指令
AngularJS: Why can't I pass DOM attribute values to the directive
是不是因为我用的是$swipe
?
HTML
<div start_slide_at="459" slide-controller></div>
JS
myApp.directive('slideController', ['$swipe',
function($swipe) {
return {
restrict: 'EA',
scope: {
start_slide_at: '='
},
link: function(scope, element, attrs) {
console.log(scope.start_slide_at);
}
}
}
]);
现在returnsundefined
:/
好吧,您可以使用 scope.startSlideAt 变量获取值。 DOM 属性中的“_”或“-”在 angularjs 的情况下转换为驼峰大小写,当在 direcitve 中使用双重绑定时。所以这是您修改后的 link 函数和范围属性。
scope: {
startSlideAt: '='
},
link: function(scope, element, attrs) {
console.log(scope.startSlideAt);
}
希望这能解决您的问题。
=
表示双向绑定。
需要范围变量而不是值
如果你只想传递值,那么创建作用域
scope: {
startSlideAt: '@'
},
和指令 convert -,_ camecase
Directives have camel cased names such as ngBind. The directive can be
invoked by translating the camel case name into snake case with these
special characters :, -, or _
所以在 link
console.log(scope.startSlideAt);
是不是因为我用的是$swipe
?
HTML
<div start_slide_at="459" slide-controller></div>
JS
myApp.directive('slideController', ['$swipe',
function($swipe) {
return {
restrict: 'EA',
scope: {
start_slide_at: '='
},
link: function(scope, element, attrs) {
console.log(scope.start_slide_at);
}
}
}
]);
现在returnsundefined
:/
好吧,您可以使用 scope.startSlideAt 变量获取值。 DOM 属性中的“_”或“-”在 angularjs 的情况下转换为驼峰大小写,当在 direcitve 中使用双重绑定时。所以这是您修改后的 link 函数和范围属性。
scope: {
startSlideAt: '='
},
link: function(scope, element, attrs) {
console.log(scope.startSlideAt);
}
希望这能解决您的问题。
=
表示双向绑定。
需要范围变量而不是值
如果你只想传递值,那么创建作用域
scope: {
startSlideAt: '@'
},
和指令 convert -,_ camecase
Directives have camel cased names such as ngBind. The directive can be invoked by translating the camel case name into snake case with these special characters :, -, or _
所以在 link
console.log(scope.startSlideAt);