Angularjs ng-controller with resolve

前端 未结 8 547
时光取名叫无心
时光取名叫无心 2021-02-01 20:30

I\'ve ran into problem with ng-controller and \'resolve\' functionality:

I have a controller that requires some dependency to be resolved before running, it works fine w

8条回答
  •  攒了一身酷
    2021-02-01 21:10

    In the below, for the route resolve, we're resolving the promise and wrapping the return data in an object with a property. We then duplicate this structure in the wrapper service ('dataService') that we use for the ng-controller form.

    The wrapper service also resolves the promise but does so internally, and updates a property on the object we've already returned to be consumed by the controller.

    In the controller, you could probably put a watcher on this property if you wanted to delay some additional behaviours until after everything was resolved and the data was available.

    Alternatively, I've demonstrated using a controller that 'wraps' another controller; once the promise from Service is resolved, it then passes its own $scope on to the wrapped controller as well as the now-resolved data from Service.

    Note that I've used $timeout to provide a 1000ms delay on the promise return, to try and make it a little more clear what's happening and when.

    angular.module('myApp', ['ngRoute'])
      .config(function($routeProvider) {
        $routeProvider
          .when('/', {
            template: '

    {{title}}

    {{blurb}}

    Using ng-controller: {{data.data}}
    ', controller: 'HomeController' }) .when('/byResolve', { template: '

    {{title}}

    {{blurb}}

    Resolved: {{data.data}}

    ', controller: "ResolveController", resolve: { dataService: ['Service', function(Service) { // Here getData() returns a promise, so we can use .then. // I'm wrapping the result in an object with property 'data', so we're returning an object // which can be referenced, rather than a string which would only be by value. // This mirrors what we return from dataService (which wraps Service), making it interchangeable. return Service.getData().then(function(result) { return { data: result }; }); } ] } }) .when('/byWrapperController', { template: '

    Wrapped: {{title}}

    {{blurb}}

    Resolving and passing to a wrapper controller: {{data.data ? data.data : "Loading..."}}
    ', controller: 'WrapperController' }); }) .controller('HomeController', function($scope) { $scope.title = "ng-controller"; $scope.blurb = "Click 'By Resolve' above to trigger the next route and resolve."; }) .controller('ResolveController', ['$scope', 'dataService', function($scope, dataService) { $scope.title = "Router and resolve"; $scope.blurb = "Click 'By ng-controller' above to trigger the original route and test ng-controller and the wrapper service, 'dataService'."; $scope.data = dataService; } ]) .controller('WrapperController', ['$scope', '$controller', 'Service', function($scope, $controller, Service) { $scope.title = "Resolving..."; //this controller could of course not show anything until after the resolve, but demo purposes... Service.getData().then(function(result) { $controller('ResolveController', { $scope: $scope, //passing the same scope on through dataService: { data: result } }); }); } ]) .service('Service', ['$timeout', function($timeout) { return { getData: function() { //return a test promise return $timeout(function() { return "Data from Service!"; }, 1000); } }; } ]) // our wrapper service, that will resolve the promise internally and update a property on an object we can return (by reference) .service('dataService', function(Service) { // creating a return object with a data property, matching the structure we return from the router resolve var _result = { data: null }; Service.getData().then(function(result) { _result.data = result; return result; }); return _result; });
    
    
    

提交回复
热议问题