问题描述
尝试基于同一行中的另一个值显示来自gridcollection的columnvalue.用户可以选择/更改模态中的值,模态中包含带有值的网格.模态关闭时,值将传回.那时,我想为也称为"设置一个值:
Trying to display a columnvalue from a gridcollection based on another value in that same row.The user can select/change values in a modal which contains a grid with values. When the modal closes the values are passed back. At that moment I would like to set a value for 'Also known as':
html:
Also known as: <input type="text" `ng-model="displayValue(displayNameData[0].show,displayNameData[0].value)">`
我在示波器上创建了一个函数,仅当'show'值为true时才选择该值:
I created a function on scope to select the value only when the 'show' value is true:
$scope.displayValue = function (show, val) {
if (show) {
return val;
}
else {
return '';
}
}
但是,当我关闭模态时,我得到一个错误:
However when I close the modal I get an error:
Error: [ngModel:nonassign] Expression 'displayValue(displayNameData[0].show,displayNameData[0].value)' is non-assignable.
plnkr参考: http://plnkr.co/edit/UoQHYwAxwdvX0qx7JFVW?p=preview
推荐答案
正如HackedByChinese所述,您无法将ng-model绑定到函数,因此请尝试如下操作:
As HackedByChinese mentioned, you can't bind ng-model to a function, so try like this:
<input type="text" ng-if="displayNameData[0].show"
ng-model="displayNameData[0].value">
或者,如果希望此控件可见,则可以创建指令,将功能添加到$parsers
中,该功能将根据show
设置空值:
Or if you want this control to be visible you can create directive, add function to $parsers
that will set empty value according to show
:
angular.module('yourModule').directive('bindIf', function() {
return {
restrict: 'A',
require: 'ngModel',
link: function(scope, element, attrs, ngModel) {
function parser(value) {
var show = scope.$eval(attrs.bindIf);
return show ? value: '';
}
ngModel.$parsers.push(parser);
}
};
});
HTML:
<input type="text" bind-if="displayNameData[0].show"
ng-model="displayNameData[0].value">
这篇关于错误:[ngModel:nonassign]表达式不可分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!