在开发中我们总是需要向服务器请求同样的数据,那么我们如何来把他们提取出来进行封装一下呢,这就需要用到服务了.
需要用到关键字factory了.
1 <!DOCTYPE html> 2 <html ng-app="factoryModule"> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 <script type="text/javascript" src="js/angular1.3.14.min.js"></script> 7 </head> 8 <body ng-controller="factoryController"> 9 <div ng-repeat="item in items"> 10 <p id="{{item.age}}">{{item.name}</p> 11 </div> 12 <script> 13 var myModule=angular.module("factoryModule",[]); 14 myModule.factory("Items", function () { 15 //正常情况下这里的数据是需要到服务器那边去获取的. 16 return [ 17 {age:23,name:"张三丰"}, 18 {age:24,name:"郭美美"}, 19 {age:22,name:"凤姐"} 20 ] 21 }); 22 myModule.controller("factoryController", function ($scope,Items) { 23 $scope.items=Items; 24 }); 25 </script> 26 </body> 27 </html>
时间: 2024-10-13 03:55:47