[英]ng-repeat in angular directive
我正在尝试创建一个带有一些额外功能的自定义选择指令。
.directive('singleSelect',function(){
restrict:'A'
scope:{
model:'='
}
link:function(elem,scope,attrs){
elem.bind('click',function(){
scope.vModel=model.slice(0,20);
});
controller:function($scope)
{
//some more manipulation with model and assign to vModel
}
template:'<ul><li ng-repeat="item in vModel"></li><ul>'
});
问题是我将值赋给vModel但它没有在模板中更新。
这是因为您正在jQuery选择器中更新范围变量。 您需要使用$ scope。$ apply来启动摘要周期,这将更新您的模型。
尝试这个:
.directive('singleSelect',function(){
restrict:'A'
scope:{
model:'='
}
link:function(scope, elem, attrs){
elem.bind('click',function(){
scope.$apply(function(){
scope.vModel=model.slice(0,20);
})
});
controller:function($scope)
{
//some more manipulation with model and assign to vModel
}
template:'<ul><li ng-repeat="item in vModel"></li><ul>'
});
注意,链接函数中的参数按顺序排列如下:link:function(scope,elem,attrs)
尝试使用$ apply方法:
elem.bind('click',function(){
scope.$apply(function(){
scope.vModel=model.slice(0,20);
});
});
捕获click事件的最佳方法是使用angular ng-click指令: https : //docs.angularjs.org/api/ng/directive/ngClick
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.