Angularjs ng-controller with resolve

前端 未结 8 641
时光取名叫无心
时光取名叫无心 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:18

    Try this

    Service:

    (function() {
    
    var myService = function($http) {
        var getData = function() {
            //return your result
        };
        return {
            getData:getData
        };
    };
    var myApp = angular.module("myApp");
    myApp.factory("myService", myService);
    }());
    

    Controller:

    (function () {
    var myApp = angular.module("myApp");
    myApp.controller('MyController', [
        '$scope', 'myService', function($scope, myService) {
            $scope.data = myService.getData();
        }
    ]);
    
    //Routing
    .when('/someUrl', {
        templateUrl : 'some.html',
        controller : 'MyController',
        resolve : {
            data: $scope.data,
        }
    })
    }());
    

提交回复
热议问题