使用ng-repeat,ng-click in angular js的问题

时间:2016-07-14 07:42:47

标签: javascript angularjs

我在Angular js的新手,我已经开始研究几个有角度的片段,但是在使用两个片段时没有得到答案。我一直在挠头理解,任何帮助都非常感谢。在下面的代码中,每当我按下按钮值不会改变

<html ng-app="secondmodule">

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
    <script>
      var demo = angular.module("secondmodule", []);
      demo.controller("controlsample", function() {
        var app = this;
        app.myname = "xyz";
        app.mysurname = "mno";
        app.myaddress = "pqr";
        app.changeme = function() {
          app.myname = "abc";
        };
      });

    </script>
  </head>

  <body ng-controller="controlsample as samp">
    <h1>hey iam {{samp.myname}}</h1>
    <h2>{{samp.myaddress}}</h2>
    <button ng-click="samp.changeme">clickme</button>
  </body>

</html>

在下面的代码中,我使用了ng-repeat,但没有生成输出

<html ng-app="mymodule">

  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
    <script>
      angular.module("mymodule", []).controller("mycontrol", function() {
        this.bikes = [{
          id: 1,
          name: 'a',
          not: 3
        }, {
          id: 4,
          name: 'b',
          not: 6
        }, {
          id: 7,
          name: 'c',
          not: 9
        }];
      });

    </script>
  </head>

  <body ng-controller="mycontrol as ctrl">
    <h1 ng-repeat=bikedemo in ctrl.bikes>
                                  <h2 ng-bind="bikedemo.id"></h2>
                                  <h2>{{bikedemo.name}}</h2>
                                  <h2>{{bikedemo.not}}</h2>
                           </h1>
  </body>

</html>

感谢您提前的帮助,学习角度建议的任何资源对我来说都很有用

5 个答案:

答案 0 :(得分:0)

第一个

$sql =  "SELECT DomainCategory.Name "
. "FROM DomainName_Client, DomainNameType, DomainCategory, OrderDomain_Client "
. "WHERE DomainName_Client.Name= ". $part1
. "AND DomainNameType.Name= " . $part2
. "AND DomainName_Client.TypeID=DomainNameType.ID "
. "AND DomainCategory.ID=DomainName_Client.DomainCategoryID "
. "AND OrderDomain_Client.DomainNameID=DomainName_Client.ID";

第二个

  <script>
    var demo=angular.module("secondmodule",[]);

     demo.controller("controlsample",function($scope)
     {

       $scope.myname="xyz";
       $scope.mysurname="mno";
       $scope.myaddress="pqr";
       $scope.changeme=function()
     {
       $scope.myname="abc";
     };
  });
</script>



<body  ng-app = "secondmodule" ng-controller="controlsample">
                  <h1 >hey iam {{myname}}</h1>
                  <h2>{{myaddress}}</h2>
                  <button ng-click="changeme()">clickme</button>
            </body>

答案 1 :(得分:0)

首先你在执行一个功能时错过了开始和结束括号

gmail

第二个用div或ul / li标签替换h1。你不能嵌套标题标记。

<button ng-click="samp.changeme()">clickme</button>

答案 2 :(得分:0)

首先:

     <html ng-app="secondmodule">
         <head>
               <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
               <script>
                       var demo=angular.module("secondmodule",[]);
                       demo.controller("controlsample",function()
                           {
                             var app=this;
                             app.myname="xyz";
                             app.mysurname="mno";
                             app.myaddress="pqr";
                             app.changeme=function()
                                 {
                                    app.myname="abc";
                                 };
                           });
                </script>
        </head>
        <body ng-controller="controlsample as samp">
              <h1 >hey iam {{samp.myname}}</h1>
              <h2>{{samp.myaddress}}</h2>
              <button ng-click="samp.changeme()">clickme</button>
        </body>

你错过了调用函数functionName()。只需添加这些括号。

对于第二个,试试这个:

     <html ng-app="mymodule">
              <head>
                    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.js"></script>
                    <script>
                           angular.module("mymodule",[]).controller("mycontrol",function()
                              {
                                this.bikes=[{id:1,name:'a',not:3},
                                            {id:4,name:'b',not:6},
                                            {id:7,name:'c',not:9}
                                           ];
                               });
                      </script>
              </head>
              <body ng-controller="mycontrol as ctrl">
                    <h1 ng-repeat =bikedemo in ctrl.bikes>
                          {{bikedemo.id"}}
                          {{bikedemo.name}}
                          {{bikedemo.not}}
                   </h1>
           </body>
       </html>

答案 3 :(得分:0)

关于学习AngularJS的资源,您可以从

开始

Shaping up with Angular.js - Code School是Angular.js基础知识的一个很好的课程。

然后你可以跟进,

A Better Way to Learn AngularJS - Thinkster以及egghead.io的课程,您也可以阅读一些书籍,本书Pro AngularJS by Adam Freeman很棒。

来自Pluralsight的课程也很棒

答案 4 :(得分:0)

您的代码有一些错误:

For 1st Code Snippet

  • 您未指定ng-app
  • 在ng-click中调用该函数应包含括号,就像您在javascript ng-click="samp.changeme()"
  • 中调用函数一样


对于第二代码片段

  • 标题标记不能与其他标题标记嵌套
  • 你的ng-repeat表达式没有用双引号括起来