AngularJS controller调用factory

news/2024/7/7 19:11:40

1、定义 factory.js 文件

var appFactorys = angular.module('starter.factorys', [])
appFactorys.factory('HouseFactory', function () {
    var 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' }
    ];
    return {
            all: function () {
                return houseList;
            },
        };
});

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页面调用

<ion-list>
    <ion-item class="item item-thumbnail-left" ng-repeat="item in houseList" href="#/housedetail/{{item.id}}" style="padding--bottom:2px">
        <img ng-src="{{item.img}}">
        <h2>{{item.title}}</h2>
        <dd class="assertive cus-price">{{item.price}}万元</dd>
        <dd class="u-f-h4">{{item.describe}}</dd>
    </ion-item>
</ion-list>



http://www.niftyadmin.cn/n/1995588.html

相关文章

Tapestry中Sumbit/ImageSubmit的属性selected和tag

Tapestry中Sumbit/ImageSubmit的属性selected和tag 个人认为Submit/ImageSubmit标准组件的两个属性selected和tag的设置比较罗嗦&#xff0c;可能是性能和灵活性的一种权衡吧。其中&#xff0c;selected指定页面类的一个属性&#xff0c;tag设置该页面属性的值。selected和tag…

centos虚拟机无法上网

centos虚拟机无法上网解决办法解决办法 首先确保使用的是nat模式 修改vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICEeth0 HWADDR00:0C:29:8D:E6:73 TYPEEthernet UUIDa81ecb00-6b42-4ff4-8895-170ab7a2672a ONBOOTyes #开机启动 NM_CONTROLLEDyes BOOTPROTOstatic# 修…

AngularJS controller调用services

1、定义 factory.js 文件 var appFactorys angular.module(starter.factorys, []) appFactorys.factory(GoodsFactory, function () {var goodsList [{ "id": 1, "title": "手机", "icon": "icon ion-android-phone-portrait c…

制作yum源

制作yum源创建一个文件夹&#xff0c;用来保存yum软件 mkdir /mnt/cdrom挂载 mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom/修改每次重启之后都要重新挂载的问题 vi /etc/fstab 添加如下配置 /dev/cdrom /mnt/cdrom iso9660 defaults 0 …

Tapestry的数据校验功能-修改框架初试

Tapestry的数据校验功能&#xff0d;修改框架初试 不需要修改tapestry框架&#xff0c;但是前端javascript的校验功能丢失了。可以满足只需要后端校验的需要。本文对“配置一次&#xff0c;前后都用”的理想模式进行实现&#xff0c;经测试表明成功了&#xff01;1,需要修改以…

经典话语03

一女子走夜路,突然看到一男张开双臂向她走来,做拥抱状,上前就是一脚.男子倒地大哭,说:都第三块了,我招谁惹谁了,带块玻璃回家就这么难么? 海龟酒量高,某天喝醉了,朋友问:你怎么还会喝醉?海龟答:唉,章鱼那孙子非要和爷划拳,丫的,那么多手,看都看不过来,真是输惨了! 一犯人被…

fastjson报错Can not find a deserializer

使用fastjson将string转成javabean异常信息解决办法异常信息 Can not find a deserializer解决办法 出现这个异常的原因是&#xff0c;实体类的字段类型不正确。如果string中的对应字段&#xff0c;还有下级元素即{key&#xff1a;{key:value}}的时候&#xff0c;实体类的字段…

IT人求职周年记:投过百度 弃过华为

IT人求职周年记&#xff1a;投过百度 弃过华为 每年这个时候&#xff0c;都是高校招聘进行得如火如荼的时候&#xff0c;那些拿着简历四处参加招聘会的同学&#xff0c;正如一年前的我&#xff0c;期待&#xff0c;惶恐&#xff0c;焦虑&#xff0c;各种心情夹杂在一起&#xf…