使用仅运行首次控制器的AngularJS服务 [英] AngularJS service only running the first time controller is used

查看:39
本文介绍了使用仅运行首次控制器的AngularJS服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下使用服务客户返回客户的控制器.问题在于,它仅在控制器第一次运行时才执行服务.查看我的服务器,如果我更改视图并说添加客户并返回列出未更新客户的视图(因为没有其他获取),则我第一次看到控制器仅在控制器使用(加载该视图)时才执行获取请求.服务的要求.

I have the following controller that uses a service Customers to return customers. The problem is that its only executing the service the first time the controller is run. Looking at my server I see its only performing the get request the FIRST time the controller used(loading that view) if I change views and say add a customer and come back to the view that list customers its not updated because there was not another get request from the service.

  .controller('searchCTRL', ['$scope', '$http', 'Customers', function($scope, $http, Customers) {
        $scope.customers = Customers;
        $scope.deleteCustomer = function(id) {
            $http.delete('/api/customer/' + id)
                .success(function(data) {
                    $scope.customers.data = data;
                })
                .error(function(data) {
                    console.log('Error: ' + data);
                });
        };
  }])

.factory('Customers', function($http){
    var Customers = {};
    $http.get('/api/customer')
        .success(function(data) {
            Customers.data = data;
        })
        .error(function(data){
            console.log('error: ' + data);
        });
    return Customers;
});

如果我停留在视图上并重新加载页面,它将获得应有的数据,但随后对该页面的任何访问均不再执行get.任何帮助将不胜感激.

if I stay on the view and reload the page it gets the data like it should but any subsequent visits to the page no longer execute the get. Any help would be appreciated.

推荐答案

Angular .factory是一个单例,因此它将始终只运行一次.同样, $ http 调用是异步的,因此您应该使用 promise 来将数据发送到控制器.请尝试以下操作:

Angular .factory is a singleton so it will always only run once. Also, the $http call is async so you should be using promise in order to get the data to your controller. Try the following:

.factory('Customers', function($http, $q){
    return function () {
        var d = $q.defer();
        $http.get('/api/customer')
            .success(function(data) {
                d.resolve(data);
            })
            .error(function(data){
                console.log('error: ' + data);
                d.reject(data);
            });
        return d.promise;
    };
});

并在您的控制器中:

.controller('searchCTRL', ['$scope', '$http', 'Customers', function($scope, $http, Customers) {
    Customers().then(function (data) {
        $scope.customers = data;
    });
...

由于 $ http 返回一个承诺,您可以通过执行以下操作进一步简化 .factory :

As $http returns a promise, you can further simply your .factory by doing:

.factory('Customers', function($http){
    return function () {
        return $http.get('/api/customer');
    };
})

有关更多详细信息,请参见有关$ http的文档.

For more detail, see documentation for $http.

这篇关于使用仅运行首次控制器的AngularJS服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆