如何使用ng-bind填充文本框?

ani*_*ean 9 angularjs

我是AngularJS的新手,我正在浏览这个链接,我很想在使用ng-bind的文本框控件上进行实验.但它没有用.

<html>
    <title>AngularJS First Application</title>
    <body>
    <h1>Sample Application</h1>
    <div ng-app="">
       <p>Enter your Name: <input type="text" ng-model="name"></p>

        <input type ="text" ng-bind="name" />
    </div>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
    </body>
    </html>
Run Code Online (Sandbox Code Playgroud)

如何使用ng-bind填充文本框?

Nic*_*ray 21

因为,ngBind属性告诉Angular 将指定HTML元素的文本内容替换为给定表达式的值,并在该表达式的值更改时更新文本内容,您将无法绑定到输入框:

<input type ="text" ng-bind="name" />
Run Code Online (Sandbox Code Playgroud)

您可以改为使用ng-value

<input type="text" ng-value="name" />
Run Code Online (Sandbox Code Playgroud)

或者您可以使用value属性中的模型填充输入框.

<input type="text" value="{{name}}" />
Run Code Online (Sandbox Code Playgroud)

看到这个codepen