[英]Why ng-repeat is not working?
ng-repeat不使用表,在輸出中只顯示標題部分? 因為我認為我所做的綁定完全沒問題,但我缺少哪些東西? 任何人都可以幫我解決我做錯的地方嗎?
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>
替換此行
$scope.incrementLikes=finction(technology)
通過
$scope.incrementLikes=function(technology)
您的代碼在myController
控制器中有拼寫錯誤。 更改finction
來function
。
正如Pankaj Parkar指出的那樣,當你遞增它們的值時,你需要糾正“finction”拼寫錯誤以及引用$ scope.technology.Likes和$ scope.technology.dislikes。
所以更新這些行:
$scope.incrementLikes=finction(technology)
{
technology.Likes++;
}
$scope.discrementLikes=function(technology)
{
technology.Dislikes++;
}
對此
$scope.incrementLikes=function(technology)
{
$scope.technology.Likes++;
}
$scope.discrementLikes=function(technology)
{
$scope.technology.Dislikes++;
}
這是完全更正的代碼。 我無法評論@pzelenovic的答案,但不要添加“$ scope.technology.Likes ++;” 或“$ scope.technology.Likes ++;” 你的增量/減量函數。 這些都很好,因為你正在更新你從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.