Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 将解析传递给车身控制器_Angularjs_Angular Ui Router - Fatal编程技术网

Angularjs 将解析传递给车身控制器

Angularjs 将解析传递给车身控制器,angularjs,angular-ui-router,Angularjs,Angular Ui Router,你好,我有这样的东西 <body ng-app="app" id="ng-app" ng-controller="MasterCtrl"> 以及一些需要主控制器来解析某些数据的视图 <div ui-view></div> im使用ui路由器 如何将resolve传递给masterctrl? 如果我尝试在状态配置中的“/”处执行主控状态之类的操作,则在向子对象发送URL时,我无法解析主控状态因此,您正在解析控制器中的某些内容。。[让我们使用“用户”]

你好,我有这样的东西

<body ng-app="app" id="ng-app" ng-controller="MasterCtrl">

以及一些需要主控制器来解析某些数据的视图

<div ui-view></div>

im使用ui路由器 如何将resolve传递给masterctrl?
如果我尝试在状态配置中的“/”处执行主控状态之类的操作,则在向子对象发送URL时,我无法解析主控状态

因此,您正在解析控制器中的某些内容。。[让我们使用“用户”]

    .state('myCoolState', {
        url: baseURL + 'coolState/:id',
        templateUrl: baseURL + 'Scripts/Templates/CoolStateView.html',
        controller: 'coolStateController',
        title: 'MyCoolState.com',
        resolve: {
            user : function (userService) {
                return userService.getUser();
            }
        }
    })
而且效果很好。。但现在您希望解析主控制器中的用户

对我有效的是,有这样一个工厂:

.factory("userService", function ($q, $http, baseURL) {
    return {
        getUser: function () {
            var deferred = $q.defer();
            $http({
                url: baseURL + 'api/User/',
                method: "GET"
            })
           .then(function (result) {
               deferred.resolve(result.data.data);
           });
           return deferred.promise;
        }
    }
});
我只是将userService注入主控制器

MasterCtrl.$inject = ['$scope', '$location', '$rootScope', '$state', '$http', 'baseURL', 'userService'];
我的主控制器定义为

var MasterCtrl = function ($scope, $location, $rootScope, $state, $http, baseURL, user) {
  user.getUser().then(function() {
    //stuff where you need access to user
    //this won't happen until 'user' is resolved
  });
};

最好在masterCtrl定义之外解析用户。。所以看起来更干净。。据我所知,你不能那样做

能否从$stateProvider中提供一些代码?最好也在states中指定控制器,而不是在body标记中指定