gpt4 book ai didi

angularjs - 错误 : [ngModel:nonassign] Expression is non-assignable

转载 作者:行者123 更新时间:2023-12-03 12:01:22 24 4
gpt4 key购买 nike

尝试根据同一行中的另一个值显示来自 gridcollection 的列值。
用户可以在包含带有值的网格的模式中选择/更改值。当模式关闭时,值将被传回。那时我想为“也称为”设置一个值:

html:

 Also known as: <input type="text" `ng-model="displayValue(displayNameData[0].show,displayNameData[0].value)">`

我在范围上创建了一个函数,仅当“显示”值为真时才选择该值:
$scope.displayValue = function (show, val) {
if (show) {
return val;
}
else {
return '';
}
}

但是,当我关闭模式时,出现错误:
Error: [ngModel:nonassign] Expression 'displayValue(displayNameData[0].show,displayNameData[0].value)' is non-assignable. 

plnkr 引用: http://plnkr.co/edit/UoQHYwAxwdvX0qx7JFVW?p=preview

最佳答案

You can bind ng-model to function

Binding to a getter/setter
Sometimes it's helpful to bind ngModel to a getter/setter function. A getter/setter is a function that returns a representation of the model when called with zero arguments, and sets the internal state of a model when called with an argument. It's sometimes useful to use this for models that have an internal representation that's different from what the model exposes to the view.



索引.html
<div ng-controller="ExampleController">
<form name="userForm">
<label>Name:
<input type="text" name="userName"
ng-model="user.name"
ng-model-options="{ getterSetter: true }" />
</label>
</form>
<pre>user.name = <span ng-bind="user.name()"></span></pre>
</div>

应用程序.js
angular.module('getterSetterExample', [])
.controller('ExampleController', ['$scope', function($scope) {
var _name = 'Brian';
$scope.user = {
name: function(newName) {
// Note that newName can be undefined for two reasons:
// 1. Because it is called as a getter and thus called with no arguments
// 2. Because the property should actually be set to undefined. This happens e.g. if the
// input is invalid
return arguments.length ? (_name = newName) : _name;
}
};
}]);

关于angularjs - 错误 : [ngModel:nonassign] Expression is non-assignable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24177707/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com