简体   繁体   English

为什么ng-repeat不起作用?

[英]Why ng-repeat is not working?

ng-repeat not working with table,in the output only header part displayed? ng-repeat不使用表,在输出中只显示标题部分? As i think the binding i did is perfectly fine, but something is there which i am missing? 因为我认为我所做的绑定完全没问题,但我缺少哪些东西? Can anyone help me out where i am doing wrong? 任何人都可以帮我解决我做错的地方吗?

JAVA SCRIPT: JAVA SCRIPT:

 var myapp=angular.module("MyApp",[]); var controller=function($scope) { var technology1=[ {Name: "C#",Likes: 0,Dislikes: 0}, {Name: "JAVA",Likes:0,Dislikes:0}, {Name: "Python",Likes:0,Dislikes:0} ]; $scope.technology=technology1; $scope.incrementLikes=finction(technology) { technology.Likes++; } $scope.discrementLikes=function(technology) { technology.Dislikes++; } } myapp.controller('MyController',controller); 
  <html ng-app="MyApp"> <head> <title></title> <script src="angular.js"></script> <script src="Day2.js"></script> </head> <Body ng-controller="MyController"> <div > <table border='2'> <thead> <tr> <th>Name Of Technology</th> <th>Likes</th> <th>Dislikes</th> <th>Likes/Dislikes</th> </tr> </thead> <tbody> <tr ng-repeat="tech in technology"> <td>{{tech.Name}}</td> <td>{{tech.Likes}}</td> <td>{{tech.Dislikes}}</td> <td> <input type="button" value="Like" ng-click="incrementLikes(tech)"> <input type="button" value="Dislikes" ng-click="decrementLikes(tech)"> </td> </tr> </tbody> </table> </div> </Body> </html> 

Replace this line 替换此行

        $scope.incrementLikes=finction(technology)

by 通过

        $scope.incrementLikes=function(technology)

Your code has a typo in your myController controller. 您的代码在myController控制器中有拼写错误。 Change finction to function . 更改finctionfunction

As Pankaj Parkar pointed out you need to correct the "finction" typo as well as to reference the $scope.technology.Likes and $scope.technology.dislikes when you are incrementing their values. 正如Pankaj Parkar指出的那样,当你递增它们的值时,你需要纠正“finction”拼写错误以及引用$ scope.technology.Likes和$ scope.technology.dislikes。

So update these lines: 所以更新这些行:

$scope.incrementLikes=finction(technology)
    {
        technology.Likes++;
    }
$scope.discrementLikes=function(technology)
    {
    technology.Dislikes++;
    }

To this 对此

$scope.incrementLikes=function(technology)
    {
        $scope.technology.Likes++;
    }
$scope.discrementLikes=function(technology)
    {
        $scope.technology.Dislikes++;
    }

Here's the fully corrected code. 这是完全更正的代码。 I can't comment on the answer from @pzelenovic but do NOT add "$scope.technology.Likes++;" 我无法评论@pzelenovic的答案,但不要添加“$ scope.technology.Likes ++;” or "$scope.technology.Likes++;" 或“$ scope.technology.Likes ++;” to your increment/decrement functions. 你的增量/减量函数。 Those are fine the way they are because you're updating the likes/dislikes property on the "tech" object you passed in from the click function. 这些都很好,因为你正在更新你从click函数传入的“tech”对象上的喜欢/不喜欢的属性。

 var myapp=angular.module("MyApp",[]); var controller=function($scope) { var technology1=[ {Name: "C#",Likes: 0,Dislikes: 0}, {Name: "JAVA",Likes:0,Dislikes:0}, {Name: "Python",Likes:0,Dislikes:0} ]; $scope.technology=technology1; $scope.incrementLikes=function(technology) { technology.Likes++; } $scope.decrementLikes=function(technology) { technology.Dislikes++; } } myapp.controller('MyController',controller); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <html ng-app="MyApp"> <head> <title></title> <script src="angular.js"></script> <script src="Day2.js"></script> </head> <Body ng-controller="MyController"> <div > <table border='2'> <thead> <tr> <th>Name Of Technology</th> <th>Likes</th> <th>Dislikes</th> <th>Likes/Dislikes</th> </tr> </thead> <tbody> <tr ng-repeat="tech in technology"> <td>{{tech.Name}}</td> <td>{{tech.Likes}}</td> <td>{{tech.Dislikes}}</td> <td> <input type="button" value="Like" ng-click="incrementLikes(tech)"> <input type="button" value="Dislikes" ng-click="decrementLikes(tech)"> </td> </tr> </tbody> </table> </div> </Body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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