详解AngularJS controller调用factory
1、定义factory.js文件
varappFactorys=angular.module('starter.factorys',[]) appFactorys.factory('HouseFactory',function(){ varhouseList=[ {id:0,title:'急售北二环小区配套齐全精装修',price:'88.0',describe:'2室1厅120平米',img:'img/ben.png'}, {id:1,title:'急售东二环小区配套齐全精装修',price:'88.0',describe:'2室1厅120平米',img:'img/max.png'}, {id:2,title:'急售南二环小区配套齐全精装修',price:'87.0',describe:'2室1厅120平米',img:'img/adam.jpg'}, {id:3,title:'急售西二环小区配套齐全精装修',price:'86.0',describe:'2室1厅120平米',img:'img/perry.png'}, {id:4,title:'急售北二环小区配套齐全精装修',price:'85.0',describe:'2室1厅120平米',img:'img/mike.png'} ]; return{ all:function(){ returnhouseList; }, }; });
2、在app.js文件引用factory.js文件
angular.module('starter',['ionic','ngCordova','starter.directives','starter.factorys','starter.services','starter.customControllers'])
3、在controller中调用factory
appControllers.controller('HouseCtrl',function($scope,$timeout,$ionicModal,$ionicActionSheet,$http,$cordovaToast,$ionicLoading,HouseFactory){ //$scope.houseList=[ //{id:0,title:'急售北二环小区配套齐全精装修',price:'88.0',describe:'2室1厅120平米',img:'img/ben.png'}, //{id:1,title:'急售东二环小区配套齐全精装修',price:'88.0',describe:'2室1厅120平米',img:'img/max.png'}, //{id:2,title:'急售南二环小区配套齐全精装修',price:'87.0',describe:'2室1厅120平米',img:'img/adam.jpg'}, //{id:3,title:'急售西二环小区配套齐全精装修',price:'86.0',describe:'2室1厅120平米',img:'img/perry.png'}, //{id:4,title:'急售北二环小区配套齐全精装修',price:'85.0',describe:'2室1厅120平米',img:'img/mike.png'} //]; /*调用Factory.js数据*/ $scope.houseList=HouseFactory.all(); })
4、html页面调用
{{item.title}}
{{item.price}}万元 {{item.describe}}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。