Javascript 服务调用后控制器中未触发角度方法

Javascript 服务调用后控制器中未触发角度方法,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,我有一个angular服务,它获取作为承诺解析的currentuser对象。我有一个由在方法调用中调用的objectuserdetails填充的部分,但不幸的是,在解析承诺后调用方法调用时没有触发 .controller('AccountCtrl', [ '$scope', 'userService', '$compile', '$http', 'utility', 'cloudService', function ($scope, userService, $compile, $htt

我有一个angular服务,它获取作为承诺解析的currentuser对象。我有一个由在方法调用中调用的objectuserdetails填充的部分,但不幸的是,在解析承诺后调用方法调用时没有触发

.controller('AccountCtrl', [
    '$scope', 'userService', '$compile', '$http', 'utility', 'cloudService', function ($scope, userService, $compile, $http, utility, cloudService) {

        $scope.userdetails = {};
        $scope.downloadPageChk = $scope.paymentHistoryPageChk = $scope.manageGroupsPageChk = "hide";
        $scope.getUserAttribute = function (param, x) {
            return userService.getAttribute(param, x);
        };
                cloudService.fetchCurrentUser().then(function (newCurrentuser)
                {
                    if (newCurrentuser)
                    {
                        $scope.currentUser = newCurrentuser;
                        $scope.getUserDetails = function()
                        {
                            if (userService && userService.isLoggedIn())
                            {
                                $scope.userdetails = JSON.parse(JSON.stringify(currentUser));
                            }
                        };

                        if (newCurrentuser == 'member') {
                            if (newCurrentuser.features.download) $scope.downloadPageChk = "show";
                            if (newCurrentuser.features.paymenthistory) $scope.paymentHistoryPageChk = "show";
                            if (newCurrentuser.group.enabled) $scope.manageGroupsPageChk = "show";
                        }
                    }
                }
])

partial
 <div data-ng-controller="AccountCtrl">
<div data-ng-init="getUserDetails()">
<input type="text" class="form-control pull-left" id="FirstName" name="FirstName" data-placeholder-attr="First Name" data-ng-model="userdetails.firstname" required>
<input type="text" class="form-control pull-left" id="LastName" name="LastName" data-placeholder-attr="Last Name" data-ng-model="userdetails.lastname" required>
</div>
</div>
.controller('AccountCtrl'[
“$scope”,“userService”,“$compile”,“$http”,“实用工具”,“cloudService”,函数($scope,userService,$compile,$http,实用工具,cloudService){
$scope.userdetails={};
$scope.downloadPageChk=$scope.paymentHistoryPageChk=$scope.manageGroupsPageChk=“hide”;
$scope.getUserAttribute=函数(参数,x){
返回userService.getAttribute(param,x);
};
cloudService.fetchCurrentUser().then(函数(newCurrentuser)
{
if(newCurrentuser)
{
$scope.currentUser=newCurrentuser;
$scope.getUserDetails=函数()
{
if(userService&&userService.isLoggedIn())
{
$scope.userdetails=JSON.parse(JSON.stringify(currentUser));
}
};
如果(newCurrentuser=='member'){
if(newCurrentuser.features.download)$scope.downloadPageChk=“show”;
if(newCurrentuser.features.paymenthistory)$scope.paymentHistoryPageChk=“show”;
如果(newCurrentuser.group.enabled)$scope.manageGroupsPageChk=“show”;
}
}
}
])
部分的
请务必让我知道我遗漏了什么。如果我将用户详细信息移出cloudservice.fetchuser().then()它至少调用了该函数,但不确定当我将其放入其中时发生了什么,我现在正在绞尽脑汁10个小时

类似的此处创建的plunk
谢谢

您在
$scope之前运行
ng init
。getUserDetails
已添加到
$scope
,因为这只发生在回调中,这就是出现问题的原因

我认为您可能过于关注一些基本示例。由于您异步获取数据,因此不需要
ng init
您可以让控制器请求服务下载数据,当数据到达时,您可以将其分配给
$scope.getUserDetails
,angular会将其显示在屏幕上

您可以看到更新的


如果我丢失了一些重要的内容,请告诉我。

您应该将函数移到
cloudservice.fetchuser().then()
方法之外。
getUserDetails
方法对于
$scope
对象不可用,直到您调用
cloudservice.fetchuser().then()
。但是您不能在您的部分代码中这样做

.controller('AccountCtrl', [
  '$scope', 'userService', '$compile', '$http', 'utility', 'cloudService', 
  function ($scope, userService, $compile, $http, utility, cloudService) {
     $scope.userdetails = {};

     $scope.getUserDetails = function(service, user) {
       if (service && service.isLoggedIn()) {
         $scope.userdetails = JSON.parse(JSON.stringify(user));
       }
     };

     $scope.downloadPageChk = $scope.paymentHistoryPageChk = $scope.manageGroupsPageChk = "hide";

     $scope.getUserAttribute = function (param, x) {
       return userService.getAttribute(param, x);
     };

     cloudService.fetchCurrentUser().then(function (newCurrentuser) {
       if (newCurrentuser) {
         $scope.currentUser = newCurrentuser;
         $scope.getUserDetails(userService, newCurrentuser);

          if (newCurrentuser == 'member') {
            if (newCurrentuser.features.download) $scope.downloadPageChk = "show";
            if (newCurrentuser.features.paymenthistory) $scope.paymentHistoryPageChk = "show";
            if (newCurrentuser.group.enabled) $scope.manageGroupsPageChk = "show";
          }
       }
     });
   });
]);

如果只想初始化数据,则不需要
$scope.getUserDetails
函数和
data ng init=“getUserDetails()”

Angular将执行服务
fetchCurrentUser
功能,并在加载时填充
$scope.userdetails

cloudService.fetchCurrentUser().then(function (newCurrentuser)
{
   if (newCurrentuser)
   {
         $scope.currentUser = newCurrentuser;
         if (userService && userService.isLoggedIn())
         {
            $scope.userdetails = JSON.parse(JSON.stringify($scope.currentUser));
         }

         if (newCurrentuser == 'member') {
            if (newCurrentuser.features.download) $scope.downloadPageChk = "show";
            if (newCurrentuser.features.paymenthistory) $scope.paymentHistoryPageChk = "show";
            if (newCurrentuser.group.enabled) $scope.manageGroupsPageChk = "show";
         }
       }
   }
}

编辑:

如果希望
getUserDetails
函数在初始化后仍可触发,则应将其置于服务方法之外:

$scope.getUserDetails = function(){
    if (userService && userService.isLoggedIn()){
       $scope.userdetails = JSON.parse(JSON.stringify($scope.currentUser));
    }
};

并根据需要触发它(通过视图中的
ng click=“getUserDetails()”
或控制器中的
$scope.getUserDetails()
).

我看不到
ng init
指令和
$scope有任何用处。getUserDetails
方法,角度双向数据绑定将处理承诺。我已经更新了相同的plunker。

可能会提到演示中的
成功
和有问题的代码中的
之间的区别。OP在响应方面对待它们是一样的seNo我无法将函数移到cloudservice.fetchuser()之外。然后(),因为$scope.userdetails对象是使用从服务返回的newCurrentuser对象填充的,而且它是一个异步调用,所以我更新了答案,以显示如何提取
getUserDetails
方法并在异步
cloudService.fetchCurrentUser.then
方法中调用它。这很有效。但我的问题是..什么如果我必须在newCurrentuser对象可用后执行一个方法,比如调用ng init?@skm在我的回答中添加了解释,这就是你的意思吗?