AngularJS controller调用services

news/2024/7/7 8:31:40

1、定义 factory.js 文件

var appFactorys = angular.module('starter.factorys', [])
appFactorys.factory('GoodsFactory', function () {
    var goodsList = [
        { "id": 1, "title": "手机", "icon": "icon ion-android-phone-portrait calm", "href": "#/homes/index" },
        { "id": 2, "title": "笔记本", "icon": "icon ion-android-laptop energized", "href": "#/homes/index" },
        { "id": 3, "title": "电脑", "icon": "icon ion-monitor assertive", "href": "#/homes/index" },
        { "id": 4, "title": "数码产品", "icon": "icon ion-android-camera balanced", "href": "#/homes/index" },
        { "id": 5, "title": "摩托车", "icon": "icon ion-stats-bars balanced", "href": "#/homes/index" },
        { "id": 6, "title": "自行车", "icon": "icon ion-android-bicycle calm", "href": "#/homes/index" },
        { "id": 7, "title": "电动车", "icon": "icon ion-stats-bars assertive", "href": "#/homes/index" },
        { "id": 8, "title": "三轮车", "icon": "icon ion-stats-bars positive", "href": "#/homes/index" },
        { "id": 9, "title": "家具", "icon": "icon ion-stats-bars assertive", "href": "#/homes/index" },
        { "id": 10, "title": "家用电器", "icon": "icon ion-stats-bars calm", "href": "#/homes/index" },
        // { "id": 11, "title": "服饰箱包", "icon": "icon ion-tshirt assertive", "href": "#/homes/index" },
        { "id": 11, "title": "服饰箱包", "icon": "icon ion-bag assertive", "href": "#/homes/index" },
        { "id": 12, "title": "母婴儿童", "icon": "icon ion-stats-bars balanced", "href": "#/homes/index" },
    ];
    return {
        all: function () {
            return goodsList;
        },
    };
});

2、定义 services.js 文件,并且调用factory函数

var appServices = angular.module('starter.services', [])
appServices.service('GoodsService', function (GoodsFactory) {
    return {
        query: function () {
            return GoodsFactory.all();
        },
    };
});

3、在 app.js 文件引用 factory.js、services.js 文件

angular.module('starter', ['ionic', 'ngCordova', 'starter.directives','starter.factorys','starter.services', 'starter.customControllers'])

4、在controller中调用services

appControllers.controller("SecondHandGoodsCtrl", function ($scope, $state, $ionicModal, $cordovaToast,GoodsService) {
    /* 调用services.js数据 */
    $scope.categoryList = GoodsService.query();
})
5、html页面调用

<div class="row row-wrap">
    <ion-item class="col col-25" ng-repeat="item in categoryList">
        <ul>
            <li>
                <a href="#/housekeeping">
                    <dt><i class="{{item.icon}}"></i></dt>
                    <dd>{{item.title}}</dd>
                </a>
            </li>
        </ul>
    </ion-item>
</div>



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

相关文章

制作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…

AngularJS 常用指令

1、directive指令改变div样式 <div id"ParentNode" class"button-bar"><div class"bgstyle" change-element>First</div><div class"bgstyle" change-element>Second</div><div class"bgstyle&…

Tapestry数据校验-修改Required Validator

Tapestry数据校验&#xff0d;修改Required Validator Required的修改遇到麻烦事&#xff0c;因为BaseValidator有一个方法isRequired&#xff0c;这样增加一个属性required的getter和 setter&#xff0c;page文件中设置requiredsearchId时&#xff0c;总是去匹配isRequired方…

netty学习之路一

netty学习之路一server端ServerHandler客户端客户端handler客户端给服务端发送数据&#xff0c;服务端再将该数据返回给客户端 server端 public class Server {public static void main(String[] args) throws Exception{EventLoopGroup bossGroup new NioEventLoopGroup(); …