简体   繁体   English

AngularJS控制器工厂休息JSON

[英]AngularJS Controller Factory rest JSON

I am trying to call a factory from multiple controllers. 我试图从多个控制器调用工厂。 This factory get JSON data. 这个工厂获得JSON数据。 I can succeed it when I tried calling from controller directly. 当我尝试直接从控制器调用时,我可以成功。 Now I just added a factory and there is be no error in console but nothing come to screen also. 现在我刚刚添加了一个工厂,控制台没有错误,但也没有任何问题。 Here is the code: 这是代码:

html template: html模板:

<!-- Schools -->
<div class="row">
    <h3 class="text-center">Schools and Associations</h3>
    <ul class="gallery gallery1" ng-controller="SchoolsCtrl">
        <li ng-repeat="school in schools" class="card">
            <a ng-href="{{school.url}}" target="_blank">
                <img ng-src="{{school.thumbUrl}}" class="img-responsive">
                <p class="text-center">{{school.name}}</p>
                <p class="text-center">{{school.time}}</p>
            </a>
        </li>
    </ul>
</div>

controller.js: controller.js:

var mainApp = angular.module('mainApp', ['bootstrapLightbox', 'ngAnimate']);

mainApp.controller('CompaniesCtrl', function($scope, GalleryService) {
$scope.companies = GalleryService.getObjects('app/common/companies.json');
});

mainApp.controller('WorksCtrl', function($scope, GalleryService) {
$scope.works = GalleryService.getObjects('app/common/freelance-projects.json');
});

mainApp.controller('SchoolsCtrl', function ($scope, GalleryService) {
$scope.schools = GalleryService.getObjects("app/common/schools.json");
});

service.js: service.js:

mainApp.factory('GalleryService', function ($http) {
    return {
        getObjects: function (jsonUrl) {
            $http.get(jsonUrl)
                .success(function (data) {
                    return data; 
                })
                .error(function (data, status, error, config) {
                    return [{heading:"Error", description: "Could not load json data"}];
                });
        }
    }
});

In order to retrieve data from factory function getObjects you should return a promise from getObjects function with .then function to chain promise. 为了从工厂函数getObjects检索数据,你应该使用.then函数从getObjects函数返回一个promise以链接promise。 Inside a controller you can make getObjects call with .then function callbacks to receive response. 在控制器内部,您可以使用.then函数回调调用getObjects来接收响应。

Factory

mainApp.factory('GalleryService', function ($http) {
    return {
        getObjects: function (jsonUrl) {
            return $http.get(jsonUrl)
                .then(function (resp) {
                    return resp.data; 
                }, function (res) {
                    return [{heading:"Error", description: "Could not load json data"}];
                });
        }
    }
});

Controller 调节器

var mainApp = angular.module('mainApp', ['bootstrapLightbox', 'ngAnimate']);

mainApp.controller('CompaniesCtrl', function($scope, GalleryService) {
  GalleryService.getObjects('app/common/companies.json').then(function(data){
    $scope.companies = data;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM