我正在尝试根据 angularjs 中的某些属性绑定(bind)未知的数组索引。
<select id="iditemtype" ng-model="method(entity, e)"
ng-options="e as e.configValue for e in allConfig() | filter:{typeName:'ItemType'}">
</select>
//javascript method inside controller
function method()
{
var index = -1;
for(var i=0;i<entity.attributes.length;i++){
if(entity.attributes[i].type=='ItemType'){
index = i;
break;
}
}
if(index==-1){
entity.attributes.push(0, e);
}
else
entity.attributes[index] = e;
}
上面的示例代码是我的真实意图。我想从选择列表中绑定(bind)特定属性。
我正在使用angularjs 1.3
最佳答案
这里你不能将函数传递给 ng-model,因为 Angular 必须能够在用户更改输入值时设置该值。在这里,您可以使用 $watch 来实现您的目标,也可以在此处使用 Angular 的 ng-change
指令和 select
。在选择值更改时调用函数,并在与 ng-change 绑定(bind)的 Controller 函数中设置相关变量。
关于javascript - 从 AngularJs 中的 Controller 绑定(bind) ngModel 或从 ngModel 调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32541536/