Angular ui-router get asynchronous data with resolve

前端 未结 4 796
忘了有多久
忘了有多久 2020-12-12 20:51

I want to display a form with data corresponding to the edited item. I use ui-router for routing. I defined a state:

myapp.config(function($stat         


        
4条回答
  •  情歌与酒
    2020-12-12 21:46

    You need to read the docs for resolve. Resolve functions are injectable, and you can use $stateParams to get the correct value from your routes, like so:

    resolve: {
        propertyData: function($stateParams, $q) {
            // The gapi.client.realestate object should really be wrapped in an
            // injectable service for testability...
    
            var deferred = $q.defer();
    
            gapi.client.realestate.get($stateParams.propertyId).execute(function(r) {
                deferred.resolve(r);
            });
            return deferred.promise;
        }
    }
    

    Finally, the values for resolve functions are injectable in your controller once resolved:

    myapp.controller('PropertyController', function($scope, propertyData) {
    
        $scope.property = propertyData;
    
    });
    

提交回复
热议问题