【问题标题】:How can I place the default value in the input text box如何在输入文本框中放置默认值
【发布时间】:2017-09-20 05:48:19
【问题描述】:

我有以下代码。我希望将默认值分配给最初显示在框中的脚本变量。稍后该值必须根据用户在文本框中的输入而改变。

    <!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”最初显示在文本框中。如何更改我的代码?

【问题讨论】:

  • 我猜你正在寻找冷杉角 js 而不是角

标签: angularjs angular-ngmodel angular-controller


【解决方案1】:

您在 html 中缺少 ng-controller 并且控制器应该是,

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

不是

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

演示

  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>

【讨论】:

    【解决方案2】:
     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>
    

    【讨论】:

      【解决方案3】:

      检查以下链接。您忘记在&lt;div&gt; 中提及您的测试控制器。

      Edited version of your code

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-18
        • 2015-09-20
        • 2013-07-27
        • 1970-01-01
        • 2012-04-23
        • 1970-01-01
        • 1970-01-01
        • 2013-09-02
        相关资源
        最近更新 更多