当我使用ng-model进行单向绑定时,会发生以下错误

时间:2017-11-08 12:06:44

标签: angularjs

当我使用ng-model进行数据绑定时,会发生以下错误 在这里,我想将输入标签中包含的字符串打印到h2标签。

Uncaught Error: Template parse errors:
Can't bind to 'ng-model' since it isn't a known property of 'input'. ("
<div>
    <label>name:
      <input [ERROR ->]ng-model="{{hero.name}}" placeholder="name">
    </label>
</div>"): ng:///AppModule/HeroesComponent.html@16:13
    at syntaxError (compiler.js:466)
    at TemplateParser.parse (compiler.js:24312)
    at JitCompiler._parseTemplate (compiler.js:33699)
    at JitCompiler._compileTemplate (compiler.js:33674)
    at eval (compiler.js:33576)
    at Set.forEach (<anonymous>)
    at JitCompiler._compileComponents (compiler.js:33576)
    at eval (compiler.js:33446)
    at Object.then (compiler.js:455)
    at JitCompiler._compileModuleAndComponents (compiler.js:33445)

4 个答案:

答案 0 :(得分:1)

删除{{}}。这样做:

<input ng-model="hero.name" placeholder="name">

希望它有所帮助。

答案 1 :(得分:0)

<input [(ngModel)]="hero.name" placeholder="name" />

更新

来自角度文档https://angular.io/guide/template-syntax#!#binding-syntax

{{expression}} [target]="expression" bind-target="expression"

是单向的。请按照文档获取更多信息。

<input [name]="hero.name" placeholder="name" />

答案 2 :(得分:0)

你只需为你的英雄(对象)声明一个类型。此外,您不必将{{}}用于角度指令,例如ng-model

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
 $scope.hero={};
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">

 <input type="text" ng-model="hero.name" placeholder="name"> <br>
 <span>The enter value is {{hero.name}}</span>
</div>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

快速建议

<ul ng-for="item of items">
<li>
<input type="text" ng-model="item.something">
</li>
</ul>