2016-05-31 23 views
1

表達式沒有通過在文本框中鍵入的值進行更新。模型表達式不能在角js中工作

<html> 
    <head> 
     <title>Angular Test</title> 
     <meta charset="UTF-8"> 
     <script src="angular.js" type="text/javascript"> </script> 
     <script> 
      var app=angular.module("myApp",[]); 
      app.controller("ctrl",function($scope){ 
       $scope.name="asdfsdf"; 
      }); 
     </script> 
    </head> 
    <body ng-app="myApp" ng-controller="ctrl" > 
     <input type="text" ng-bind="name" /> {{name}} 
    </body> 
</html> 

回答

1

將ng-ng更改爲ng-model ..... ng-model提供雙向綁定,其中ng-bind僅提供單向綁定。

<html> 
    <head> 
     <title>Angular Test</title> 
     <meta charset="UTF-8"> 
     <script src="angular.js" type="text/javascript"> </script> 
     <script> 
      var app=angular.module("myApp",[]); 
      app.controller("ctrl",function($scope){ 
       $scope.name="asdfsdf"; 
      }); 
     </script> 
    </head> 
    <body ng-app="myApp" ng-controller="ctrl" > 
     <input type="text" ng-model="name" /> {{name}} 
    </body> 
</html> 
0

更改ng-bindng-model更新輸入的值。

<html> 
 

 
<head> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
    <title>Angular Test</title> 
 
    <meta charset="UTF-8"> 
 
    <script src="angular.js" type="text/javascript"> 
 
    </script> 
 
    <script> 
 
    var app = angular.module("myApp", []); 
 
    app.controller("ctrl", function($scope) { 
 
     $scope.name = "asdfsdf"; 
 
    }); 
 
    </script> 
 
</head> 
 

 
<body ng-app="myApp" ng-controller="ctrl"> 
 
    <input type="text" ng-model="name" />{{name}} 
 
</body> 
 

 
</html>