2017-09-20 44 views
-2

我有以下代碼。我希望將默認值分配給最初顯示在框中的腳本變量。之後,值必須根據用戶在文本框中的輸入而改變。如何將默認值放在輸入文本框中

<!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="UTF-8"> 
     <title> Hello app </title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script> 
    </head> 
    <body> 
    <div ng-app="testapp"> 
     <p> Enter your name: <input type="text" ng-model="name"></p> 
     <p> Enter your age here: <input type="text" ng-model="age"> </p> 
     <ol> 
      <li> My Name is {{ name }} </li> 
      <li>I am {{ age}} years old </li> 
     </ol> 

     <script> 
     var app = angular.module("testapp",[]); 
     app.controller=("test", function($scope){ 
     $scope.age = "20" 
     $scope.name = "zigo" 
     }); 
     </script> 
    </div> 
    </body> 
    </html> 

我想要「20」和「zigo」最初顯示在文本框中。我如何更改我的代碼?

+0

我猜你正在尋找杉木角JS沒有棱角 –

回答

3

你缺少在HTML ng-controller和控制器應,

app.controller("test", function($scope){ 

app.controller=("test", function($scope){ 

DEMO

var app = angular.module("testapp",[]); 
 
app.controller("test", function($scope){ 
 
     $scope.age = "20" 
 
     $scope.name = "zigo" 
 
});
<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
     <meta charset="UTF-8"> 
 
     <title> Hello app </title> 
 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script> 
 
    </head> 
 
    <body> 
 
    <div ng-app="testapp" ng-controller="test"> 
 
     <p> Enter your name: <input type="text" ng-model="name"></p> 
 
     <p> Enter your age here: <input type="text" ng-model="age"> </p> 
 
     <ol> 
 
      <li> My Name is {{ name }} </li> 
 
      <li>I am {{ age}} years old </li> 
 
     </ol> 
 
    
 
    </div> 
 
    </body> 
 
    </html>

0
var app = angular.module("testapp",[]); 
app.controller("test", function($scope){ 
     $scope.age = "20" 
     $scope.name = "zigo" 
}); 
<!DOCTYPE html> 
    <html> 
    <head> 
     <meta charset="UTF-8"> 
     <title> Hello app </title> 
     <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script> 
    </head> 
    <body> 
    <div ng-app="testapp" ng-controller="test"> 
     <p> Enter your name: <input type="text" ng-model="name"></p> 
     <p> Enter your age here: <input type="text" ng-model="age"> </p> 
     <ol> 
      <li> My Name is {{ name }} </li> 
      <li>I am {{ age}} years old </li> 
     </ol> 

    </div> 
    </body> 
    </html> 
相關問題