[英]Can someone please explain me and simplify this particular function addButton() that I have used in angular js
angular.module('myApp',[]) .controller('myController', myController) .factory('menuCtrlFactory', menuCtrlFactory); myController.$inject =['menuCtrlFactory']; function myController(menuCtrlFactory){ var add=this; var shoppingList= menuCtrlFactory(); add.itemss= shoppingList.getItems(); add.addbutton=function(item){ shoppingList.addbutton(item); } } function menuCtrlService(){ var service= this; var itemss=[{ dish:'Uthapizza', category: 'mains', label:'Hot', price:'4.99', description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', comments:[] }, { dish:'Uthapizza', category: 'mains', label:'Hot', price:'4.99', description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.', comments:[] }, ]; service.addbutton=function(item){ item.comments.push(item.newComment); }; service.getItems= function(){ return itemss; }; } function menuCtrlFactory(){ var factory= function(){ return new menuCtrlService(); } return factory; }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <!DOCTYPE html> <html ng-app='myApp'> <head> <meta charset="utf-8"> <script src="angular.min.js"></script> <title>Shopping Menu</title> </head> <body> <h1>Food Menu</h1> <div ng-controller="myController as menuList"> <ul> <li ng-repeat="l in menuList.itemss"> <h2>{{l.dish}} <span>{{l.label}}</span> <span>{{l.price | currency}}</span> </h2> <p>{{l.category}}</p> <p>{{l.description}}</p> <p ng-repeat="comment in l.comments track by $index">{{comment}}</p> <p>Enter Your Comments: <input type="text" ng-model="l.newComment"> <button ng-click="menuList.addbutton(l);">Add Comments</button></p> </li> </ul> </div> </body> </html>
我是angularJS的新手。 實際上,這是我做過的一個示例程序。我已經理解了代碼,但是在這行代碼中我無法理解
service.addbutton =函數(項目){
item.comments.push(item.newComment);
}
是用作功能變量的“項目”。 或者有人可以向我解釋該函數的內部功能。我很困惑
函數內部引用的item
是作為輸入參數傳遞到addbutton函數中的對象。 函數內部的代碼行采用新添加的注釋,並將其添加到現有注釋列表中。
item.comments.push(item.newComment);
很好。
但是,根據OP的要求將該行分成多行以提高可讀性。
// Get the reference to all existing comments
var existingComments = item.comments;
// Get the reference to the new Comment
var newComment = item.newComment;
// Add new comment to existing comments
existingComments.push(newComment);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.