AngularJS controller calls services

1. Define the factory.js file

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. Define the services.js file and call the factory function

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

3. Refer to the factory.js and services.js files in the app.js file

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

4. Call services in the controller

appControllers.controller("SecondHandGoodsCtrl", function ($scope, $state, $ionicModal, $cordovaToast,GoodsService) {
    /* call services.js data */
    $scope.categoryList = GoodsService.query();
})
5, html page call

<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>


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325890649&siteId=291194637