错误:ngModel:不可分配的表达式
Error: ngModel: Non-Assignable Expression
我有动态生成下拉列表的代码。我想使用ng重复设置选定的值,为此我使用了ng模型中的函数,但我得到了
Error: [ngModel:nonassign] Expression 'item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)' is non-assignable. Element: <select class="form-control input-sm" ng-model="item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)" name="blockName" ng-options="choice.id as choice.value for choice in blockNameOptions">
HTML
<div ng-repeat="item in modulesData.blocks track by item.id">
<select class="form-control input-sm" ng-model="item.blockName = getCurrentComboBoxValue(item.blockName.id, blockNameOptions)" name="blockName"
ng-options="choice.id as choice.value for choice in blockNameOptions">
</select>
</div>
控制器
$scope.getCurrentComboBoxValue = function (id, availableData) {
var result = _.where(availableData, { 'id': id });
return result[0];
}
getCurrentComboBoxValue(blockNameOptions)
在=
的左侧无效。ngModel要求左边的东西是可赋值的。
我认为您正在尝试做angular为您所做的事情。如果为angular指定一个特性的"名称"以将选定的值指定给它,则会自动绑定该值。当它发生变化时。在您的情况下,selectedValue将为choice.id
。这个值可以是作用域上现有的东西,也可以将其分配给作用域。例如:
<div ng-repeat="item in modulesData.blocks track by item.id">
<select class="form-control input-sm" ng-model="selectedItem" name="blockName"
ng-options="choice.id as choice.value for choice in blockNameOptions">
</select>
<div>I selected {{selectedItem}}</div>
</div>
在您的Ctrl中,您可以执行
$scope.getCurrentComboBoxValue = function (id, availableData) {
console.log($scope.selectedItem);
}
如果您默认为$scope.selectedBlock=,则它会默认为您选择。
相关文章:
- 错误:[$compile:nonassign]表达式'未定义'与指令'内容可编辑'是不可
- 如何发送分配列表<字符串>到JavaScript数组或可枚举对象
- Jslint 错误:需要条件表达式,而是看到分配
- 使用一个表达式将两个变量分配给相同的值
- 当数组在angularjs中没有可比较的值时,如何分配默认值
- 是否存在将String转换为可执行的c++表达式的方法
- 正则表达式使链接可点击(仅在“href”而不是“img src”中)
- 使用 HTML 输出标记查看可缩放图像并分配 src
- 用Javascript正则表达式替换iframe到可点击的YouTube链接
- 逻辑表达式可视化工具
- 如何将变量表达式转换为可执行代码(而不是替换值)
- 如何将行数计数分配给另一个值,该值稍后可在Protractor中的测试用例中使用
- 我是妄想,还是 JS 正则表达式支持可选的重复边界
- 将cron表达式转换为人类可读字符串的JavaScript API
- 正则表达式 JavaScript 捕获直到(之前)可选字符串
- 你能在javascript中分配一个引用表达式吗?
- 正则表达式 - 使用可选国家/地区代码验证手机
- 正则表达式终止具有可选文本的匹配组
- Nodejs 可选正则表达式命名组的插件或代码片段
- Javascript正则表达式可选字符