如何在解决全局数据后重新运行控制器?

时间:2022-07-13 15:44:41

I resolve data on application load in a run block...

我在运行块中解析有关应用程序负载的数据...

.run(function($rootScope, $q, teams, schools, news, games){
  // go out and grab all the relevant data
  $rootScope.showSplash = true;
  $q.all([
    $rootScope.school  = schools.get({id:1}),
    $rootScope.teams   = teams.query({id:1}),
    $rootScope.news    = news.query({id:1}),
    $rootScope.games   = games.query({id:1})
  ]).then(function(){
    setTimeout(function(){
      $rootScope.showSplash = false;
      $rootScope.$digest();
    }, 1000);
  })
})

I have a controller whose scope should clone the data via $rootScope...

我有一个控制器,其范围应该通过$ rootScope克隆数据...

.controller('NewsDetailCtrl', function ($scope, $routeParams) {
    $scope.newss = $scope.news.filter(function(news){
        return news.id == $routeParams.id;
    }).shift();
});

If the user is on the new-detail.html page, no data is present because the $scope clones an empty array. Is it possible to rerun the controllers when that information is received?

如果用户位于new-detail.html页面上,则不会出现任何数据,因为$ scope克隆了一个空数组。收到该信息后是否可以重新运行控制器?

1 个解决方案

#1


2  

For a better solution you could create a decorated cache.

要获得更好的解决方案,您可以创建装饰缓存。

Take a look at the decorator provider in angularJS: https://docs.angularjs.org/api/auto/service/$provide

看看angularJS中的装饰提供者:https://docs.angularjs.org/api/auto/service/$provide

This will allow you to wrap your biz logic in the service. Then decorate that service with a caching layer in order to make sure that it is only retrieved once. I can post an example if needed.

这将允许您将业务逻辑包装在服务中。然后使用缓存层装饰该服务,以确保仅检索一次。如果需要,我可以发布一个例子。

** UPDATE TO ADD EXAMPLE **

**更新添加示例**

var upstream = angular.module('thirdParty', []);
upstream.service('emailService', function() {
  this.email = "";

  this.setContent = function(content) {
    this.email = content;
  };

  this.send = function(recipient) {
    return 'sending "' + this.email + '" to ' + recipient;
  };
});

var app = angular.module('myApp', ['thirdParty']);

app.config(function($provide) {
  $provide.decorator('emailService', function($delegate) {
    // myApp depends on the emailService from a third-party module, but the service is lacking a way to send email with signature.
    // To avoid reinventing the wheel and, as well as, maintaining a good habit of leaving third-party module intact, 
    // I use $provide.decorator here to enhance emailService.
    $delegate.sendWithSignature = function(recipient, signature) {
      return 'sending "' + this.email + '" to ' + recipient + " by " + signature;
    };
    return $delegate;
  });
});

app.controller('MainCtrl', function($scope, emailService) {
  emailService.setContent("Greeting!!");
  $scope.emailComplete = emailService.sendWithSignature('a@a.com', 'tamakisquare');
});

I've added an example usage of a decorator. Hopefully that helps

我添加了一个装饰器的示例用法。希望这会有所帮助

#1


2  

For a better solution you could create a decorated cache.

要获得更好的解决方案,您可以创建装饰缓存。

Take a look at the decorator provider in angularJS: https://docs.angularjs.org/api/auto/service/$provide

看看angularJS中的装饰提供者:https://docs.angularjs.org/api/auto/service/$provide

This will allow you to wrap your biz logic in the service. Then decorate that service with a caching layer in order to make sure that it is only retrieved once. I can post an example if needed.

这将允许您将业务逻辑包装在服务中。然后使用缓存层装饰该服务,以确保仅检索一次。如果需要,我可以发布一个例子。

** UPDATE TO ADD EXAMPLE **

**更新添加示例**

var upstream = angular.module('thirdParty', []);
upstream.service('emailService', function() {
  this.email = "";

  this.setContent = function(content) {
    this.email = content;
  };

  this.send = function(recipient) {
    return 'sending "' + this.email + '" to ' + recipient;
  };
});

var app = angular.module('myApp', ['thirdParty']);

app.config(function($provide) {
  $provide.decorator('emailService', function($delegate) {
    // myApp depends on the emailService from a third-party module, but the service is lacking a way to send email with signature.
    // To avoid reinventing the wheel and, as well as, maintaining a good habit of leaving third-party module intact, 
    // I use $provide.decorator here to enhance emailService.
    $delegate.sendWithSignature = function(recipient, signature) {
      return 'sending "' + this.email + '" to ' + recipient + " by " + signature;
    };
    return $delegate;
  });
});

app.controller('MainCtrl', function($scope, emailService) {
  emailService.setContent("Greeting!!");
  $scope.emailComplete = emailService.sendWithSignature('a@a.com', 'tamakisquare');
});

I've added an example usage of a decorator. Hopefully that helps

我添加了一个装饰器的示例用法。希望这会有所帮助