admin管理员组文章数量:1426058
I have a scenario in which the property ng-model should bind to value that es from database as a part of business logic.
For simplicity, I have created this example
function TodoCtrl($scope) {
$scope.field1 = "PropertyFromDB";
$scope.PropertyFromDB = "hello world";
}
<div ng-app>
<h2>ngModel BINDING EXAMPLE</h2><br/>
<div ng-controller="TodoCtrl">
<div ng-init="imod = field1">
<input type="text" ng-model="imod"></input>
</div>
</div>
</div>
In this example, field1
is the value which will be property that es from DB (i.e. PropertyFromDB) and ng-model should bind to PropertyFromDB instead of field1.
So, its like I want to evaluate expression inside expression syntax of ng-model but I am unable to do so.
Thanks
I have a scenario in which the property ng-model should bind to value that es from database as a part of business logic.
For simplicity, I have created this example
function TodoCtrl($scope) {
$scope.field1 = "PropertyFromDB";
$scope.PropertyFromDB = "hello world";
}
<div ng-app>
<h2>ngModel BINDING EXAMPLE</h2><br/>
<div ng-controller="TodoCtrl">
<div ng-init="imod = field1">
<input type="text" ng-model="imod"></input>
</div>
</div>
</div>
In this example, field1
is the value which will be property that es from DB (i.e. PropertyFromDB) and ng-model should bind to PropertyFromDB instead of field1.
So, its like I want to evaluate expression inside expression syntax of ng-model but I am unable to do so.
Thanks
Share Improve this question edited Jul 19, 2015 at 13:15 Pankaj Parkar 136k23 gold badges240 silver badges303 bronze badges asked May 1, 2015 at 17:30 miztakenmiztaken 1553 silver badges10 bronze badges2 Answers
Reset to default 4You can refer to the current scope with this
and read its property, like:
<div ng-init="imod = field1">
<input type="text" ng-model="this[imod]"></input>
</div>
but it is a very unusual and inconvenient way to create a View Model.
I suggest creating a better structure/object that contains the field/value bination and setting that object on the scope, instead of setting field and value separately the scope.
You simply cannot set value of ng-model
by using any expression. The alternative would be to change the stucture of the JSON which you returned from server.
Markup
<div ng-controller="TodoCtrl">
<div ng-repeat="field in properties">
<label>{{field.property}}</label>
<input type="text" ng-model="field.value" />
</div>{{properties}}
</div>
Code
function TodoCtrl($scope) {
$scope.properties = [{
property: 'PropertyFromDB',
value: "hello world"
}, {
property: 'PropertyFromDB1',
value: "hello world1"
}]
}
Fiddle Here
本文标签: javascriptBinding ngModel to expression39s valueStack Overflow
版权声明:本文标题:javascript - Binding ngModel to expression's value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745416151a2657694.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论