Angularjs 承诺';然后';在使用';用户界面路由器&x27;

Angularjs 承诺';然后';在使用';用户界面路由器&x27;,angularjs,angular-ui-router,Angularjs,Angular Ui Router,配置: .state('index.usermng.userInfo',{ url: '/userInfo/:id', templateUrl: 'tpls/userInfo.html', controller: 'userInfoCtrl', resolve: { userInfo: function($http, serverUrl, $stateParams){ retu

配置:

.state('index.usermng.userInfo',{
        url: '/userInfo/:id',
        templateUrl: 'tpls/userInfo.html',
        controller: 'userInfoCtrl',
        resolve: {
            userInfo: function($http, serverUrl, $stateParams){
                return $http.get(serverUrl + "/user/info?userId=" + $stateParams.id);
            }
        }
    })
控制员:

//UsrInfo Controller
userApp.controller('userInfoCtrl', ['$scope', 'userInfo', function($scope, userInfo) {
userInfo.then(function(response) {
    $scope.data = response.data
})

我在'ui router'config.js中定义了'userInfo',当我想在控制器中使用它时,会导致未定义的'then',首先,您的解决方案应该是:

userInfo.$promise.then(function(response) {
    $scope.data = response
})
发件人:

请注意,$routeParams仅在路线更改成功完成后更新。这意味着,您不能指望$routeParams在路由解析功能中是正确的。相反,您可以使用$route.current.params访问新路由的参数

所以只需注入
$route
,而不是
$routeParams

resolve: {
    userInfo: function($http, serverUrl,  $route){
      return $http.get(serverUrl + "/user/info?userId=" + $route.current.params.id)
      .then(function(response){
        return response.data;
      })
    }
}
通过这种方式,
解析函数
将返回
响应
,因此在控制器中,您可以:

$scope.data = userInfo
更多关于承诺 如果您的
resolve函数
返回一个
promise
(在您的情况下不是这样),则可以使用$promise访问该函数

承诺示例(使用
$resource
) 从

成功后,将使用相同的资源实例或 集合对象,已使用来自服务器的数据更新

因此,在这种情况下,控制器中应:

userInfo.$promise.then(function(response) {
    $scope.data = response
})

首先,你的决心应该是:

userInfo.$promise.then(function(response) {
    $scope.data = response
})
发件人:

请注意,$routeParams仅在路线更改成功完成后更新。这意味着,您不能指望$routeParams在路由解析功能中是正确的。相反,您可以使用$route.current.params访问新路由的参数

所以只需注入
$route
,而不是
$routeParams

resolve: {
    userInfo: function($http, serverUrl,  $route){
      return $http.get(serverUrl + "/user/info?userId=" + $route.current.params.id)
      .then(function(response){
        return response.data;
      })
    }
}
通过这种方式,
解析函数
将返回
响应
,因此在控制器中,您可以:

$scope.data = userInfo
更多关于承诺 如果您的
resolve函数
返回一个
promise
(在您的情况下不是这样),则可以使用$promise访问该函数

承诺示例(使用
$resource
) 从

成功后,将使用相同的资源实例或 集合对象,已使用来自服务器的数据更新

因此,在这种情况下,控制器中应:

userInfo.$promise.then(function(response) {
    $scope.data = response
})

首先,你的决心应该是:

userInfo.$promise.then(function(response) {
    $scope.data = response
})
发件人:

请注意,$routeParams仅在路线更改成功完成后更新。这意味着,您不能指望$routeParams在路由解析功能中是正确的。相反,您可以使用$route.current.params访问新路由的参数

所以只需注入
$route
,而不是
$routeParams

resolve: {
    userInfo: function($http, serverUrl,  $route){
      return $http.get(serverUrl + "/user/info?userId=" + $route.current.params.id)
      .then(function(response){
        return response.data;
      })
    }
}
通过这种方式,
解析函数
将返回
响应
,因此在控制器中,您可以:

$scope.data = userInfo
更多关于承诺 如果您的
resolve函数
返回一个
promise
(在您的情况下不是这样),则可以使用$promise访问该函数

承诺示例(使用
$resource
) 从

成功后,将使用相同的资源实例或 集合对象,已使用来自服务器的数据更新

因此,在这种情况下,控制器中应:

userInfo.$promise.then(function(response) {
    $scope.data = response
})

首先,你的决心应该是:

userInfo.$promise.then(function(response) {
    $scope.data = response
})
发件人:

请注意,$routeParams仅在路线更改成功完成后更新。这意味着,您不能指望$routeParams在路由解析功能中是正确的。相反,您可以使用$route.current.params访问新路由的参数

所以只需注入
$route
,而不是
$routeParams

resolve: {
    userInfo: function($http, serverUrl,  $route){
      return $http.get(serverUrl + "/user/info?userId=" + $route.current.params.id)
      .then(function(response){
        return response.data;
      })
    }
}
通过这种方式,
解析函数
将返回
响应
,因此在控制器中,您可以:

$scope.data = userInfo
更多关于承诺 如果您的
resolve函数
返回一个
promise
(在您的情况下不是这样),则可以使用$promise访问该函数

承诺示例(使用
$resource
) 从

成功后,将使用相同的资源实例或 集合对象,已使用来自服务器的数据更新

因此,在这种情况下,控制器中应:

userInfo.$promise.then(function(response) {
    $scope.data = response
})

这是我设置解析的方式,看看这是否适用于您:

resolve: {
    userInfo: function($http, serverUrl, $stateParams){
        return $http.get(serverUrl + "/user/info?userId=" + $stateParams.id).then(function(response){
return response.data});
    }
}

然后在您的控制器中,
userInfo
被解析,您无需等待它被解析。

这就是我设置解析的方式,看看这是否适用于您:

resolve: {
    userInfo: function($http, serverUrl, $stateParams){
        return $http.get(serverUrl + "/user/info?userId=" + $stateParams.id).then(function(response){
return response.data});
    }
}

然后在您的控制器中,
userInfo
被解析,您无需等待它被解析。

这就是我设置解析的方式,看看这是否适用于您:

resolve: {
    userInfo: function($http, serverUrl, $stateParams){
        return $http.get(serverUrl + "/user/info?userId=" + $stateParams.id).then(function(response){
return response.data});
    }
}

然后在您的控制器中,
userInfo
被解析,您无需等待它被解析。

这就是我设置解析的方式,看看这是否适用于您:

resolve: {
    userInfo: function($http, serverUrl, $stateParams){
        return $http.get(serverUrl + "/user/info?userId=" + $stateParams.id).then(function(response){
return response.data});
    }
}

然后在您的控制器中,
userInfo
被解析,您无需等待它被解析。

谢谢,当我按照您告诉我的方式修改时,导致:'TypeError:Cannot read property'然后'of undefined'谢谢,当我按照您告诉我的方式修改时,导致:'TypeError:Cannot read property'然后'of undefined'谢谢,当我按照你告诉我的方式修改时,导致:'TypeError:Cannot read property'then'of undefined'Thank',当我按照你告诉我的方式修改时,导致:'TypeError:Cannot read property'then'of undefined'Thank',Thank'of undefined'Thank,它起作用了,但我仍然不明白为什么我不能在控制器中使用承诺,你能给我解释一下吗?只有在解析userInfo时才会调用控制器,所以当控制器中的代码运行时,userInfo已经被解析了。因此,在您的代码中,userInfo是响应对象,数据在userInfo.data中。谢谢,它可以工作,但我仍然不明白为什么我不能在控制器中使用承诺,您能为我解释一下吗?只有在解析userInfo时才调用控制器,所以当您的控制器中的代码运行时,userInfo已经解析。因此,在您的代码中,userInfo是响应对象,数据在userInfo.data中。谢谢,它是