简体   繁体   中英

Issue in using ng-repeat,ng-click in angular js

Iam newbie in Angular js,I have started working on few snippets in angular but didnt get answers while working with two snippets.I have been scratching my head to understand ,Any help is greatly appreciated.In the below code whenever I am pressing the button the value isnt getting changed

<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>

In the below code I have used ng-repeat ,but no output is being generated

<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>

Thanks for your help in advance,Any resources suggested in learning angular will be of great use to me

first one

  <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>

second one

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

1st you miss open and closing bracket when execute a function

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

2nd replace h1 with div or ul/li tag. you could not nested header tag.

<div ng-repeat="bikedemo in ctrl.bikes">
  <h2 ng-bind="bikedemo.id"></h2>
  <h2>{{bikedemo.name}}</h2>
  <h2>{{bikedemo.not}}</h2>
</div>

For first :

     <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>

You missed calling the function ie functionName() . Just add those brackets.

For the second one, try this :

     <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>

Regarding the resources to learn AngularJS you can start with,

Shaping up with Angular.js - Code School a great course for Angular.js fundamentals.

Then you can follow up with,

A Better Way to Learn AngularJS - Thinkster and courses from egghead.io and you can read some book too, this book Pro AngularJS by Adam Freeman is great.

The courses from Pluralsight are great too

Your code has some errors:

For 1st Code Snippet

  • You did not specified your ng-app
  • Calling the function in ng-click should contain parenthesis as if you're calling a function in javascript ng-click="samp.changeme()"


For 2nd Code Snippet

  • Header tags cannot be nested with other header tags
  • Your ng-repeat expression is not enclosed in double quotes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM