Javascript 如何使用ng repeat和ng模型将动态值绑定到输入?
在将值绑定到angular中的输入时,我遇到了一些问题。我首先初始化了“object[component.name]”,然后将该值分配给ng模型。我这样做是因为我必须绑定来自ng-repeat的值。但是它给了我错误。以下是我的代码片段。如有任何帮助,将不胜感激Javascript 如何使用ng repeat和ng模型将动态值绑定到输入?,javascript,angularjs,angularjs-ng-repeat,ng-repeat,Javascript,Angularjs,Angularjs Ng Repeat,Ng Repeat,在将值绑定到angular中的输入时,我遇到了一些问题。我首先初始化了“object[component.name]”,然后将该值分配给ng模型。我这样做是因为我必须绑定来自ng-repeat的值。但是它给了我错误。以下是我的代码片段。如有任何帮助,将不胜感激 <div ng-repeat="component in reportTemplate" class="inputFieldSection inputFieldTitle" ng-if="component.type == 'tex
<div ng-repeat="component in reportTemplate" class="inputFieldSection inputFieldTitle" ng-if="component.type == 'text'">
<label class="item item-input">
<input type="text" name={{component.name}} ng-init="object[component.name]={{component.name}}" ng-model="object[component.name]" ng-focus="clearValidation();" max-length="50" required placeholder="{{component.label}}">
</label>
<p ng-show="createReportForm.{{component.name}}.$error.required">Please Enter {{component.name}}</p>
</div>
请输入{{component.name}
这是我在控制台中得到的错误
ionic.bundle.js:25510 Error: [$parse:syntax] Syntax Error: Token '{' invalid key at column 25 of the expression [object[component.name]={{component.name}}] starting at [{component.name}}].
http://errors.angularjs.org/1.4.3/$parse/syntax?p0=%7B&p1=invalid%20key&p2=…Bcomponent.name%5D%3D%7B%7Bcomponent.name%7D%7D&p4=%7Bcomponent.name%7D%7D
at http://localhost:8100/lib/ionic/js/ionic.bundle.js:13248:12
at Object.AST.throwError (http://localhost:8100/lib/ionic/js/ionic.bundle.js:26061:11)
at Object.AST.object (http://localhost:8100/lib/ionic/js/ionic.bundle.js:26048:16)
at Object.AST.primary (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25956:22)
at Object.AST.unary (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25944:19)
at Object.AST.multiplicative (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25931:21)
at Object.AST.additive (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25922:21)
at Object.AST.relational (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25913:21)
at Object.AST.equality (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25904:21)
at Object.AST.logicalAND (http://localhost:8100/lib/ionic/js/ionic.bundle.js:25896:21) <input type="text" name="{{component.name}}" ng-init="object[component.name]={{component.name}}" ng-model="object[component.name]" ng-focus="clearValidation();" max-length="50" required="" placeholder="{{component.label}}" class="ng-pristine ng-untouched ng-valid">
ionic.bundle.js:25510错误:[$parse:syntax]语法错误:从[{component.name}]开始的表达式[object[component.name]={{{component.name}]第25列的标记“{”无效键。
http://errors.angularjs.org/1.4.3/$parse/syntax?p0=%7B&p1=无效的%20key&p2=…b组件。名称%5D%3D%7B%7B组件。名称%7D%7D&p4=%7B组件。名称%7D%7D
在http://localhost:8100/lib/ionic/js/ionic.bundle.js:13248:12
在Object.AST.thrower(http://localhost:8100/lib/ionic/js/ionic.bundle.js:26061:11)
at Object.AST.Object(http://localhost:8100/lib/ionic/js/ionic.bundle.js:26048:16)
在Object.AST.primary(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25956:22)
至少一元(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25944:19)
在Object.AST.乘法(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25931:21)
至少(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25922:21)
at Object.AST.relational(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25913:21)
在Object.AST.equality(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25904:21)
at Object.AST.logicalAND(http://localhost:8100/lib/ionic/js/ionic.bundle.js:25896:21)
ng show
指令中不应包含{{}
(插值指令)。您可以使用objectindex
访问createReportForm
对象
ng-show="createReportForm[component.name].$error.required"
而且ng init
也有同样的错误ng init=“object[component.name]=component.name”
不确定为什么要使用ng init
指令,应避免使用该指令
ng show
指令中不应包含{{}
(插值指令)。您可以使用objectindex
访问createReportForm
对象
ng-show="createReportForm[component.name].$error.required"
而且ng init
也有同样的错误ng init=“object[component.name]=component.name”
不确定为什么要使用ng init
指令,应避免使用该指令
错误似乎出现在输入字段绑定中。我已经尝试了您的建议,但仍然得到了相同的错误。@pritamkale错误堆栈跟踪清楚地表明,错误发生在
input
fieldng init
指令上,请尝试按照我建议的那样更改它。谢谢:)@pritamkale您能在这方面取得任何进展吗?错误似乎在输入字段绑定中。我已经尝试了您的建议,但仍然得到了相同的错误。@pritamkale错误堆栈跟踪清楚地表明,错误发生在input
fieldng init
指令上,请尝试按照我建议的那样更改它。谢谢:)@pritamkale你在这方面有什么进展吗?