Angularjs 在ui路由器解析中使用$scope

Angularjs 在ui路由器解析中使用$scope,angularjs,angularjs-scope,angular-ui-router,Angularjs,Angularjs Scope,Angular Ui Router,我正在使用ui路由器解析,以便从服务获取一些数据 问题是,我需要从父$scope获取一个值,以便调用如下所示的服务 resolve: { contactService: 'contactService', contacts: function ($scope, contactService) { return contactService.getContacts($scope.parentCtrl.par

我正在使用ui路由器解析,以便从服务获取一些数据

问题是,我需要从父$scope获取一个值,以便调用如下所示的服务

resolve: {
              contactService: 'contactService',
              contacts: function ($scope, contactService) {
                  return contactService.getContacts($scope.parentCtrl.parentObjectId);
              }
          }
我不断得到
错误:[$rootScope:infdig]10$digest()迭代次数。流产

还尝试了一些不顾一切的尝试,例如向resolve对象添加作用域,如下图所示,但没有成功

scope: $scope

有什么想法吗?

这是不可能的,此时范围尚未初始化,因此您无法在resolve对象中使用它。您可以在控制器中的作用域初始化后访问它。解析的全部要点是,它在控制器初始化之前运行,以便您可以插入并直接访问范围中解析的项

如果需要将变量传递到下一个状态,可以使用可在resolve中使用的
$stateParams
对象来实现。您可以在更改状态时向其添加数据,例如:

在模板中,如果您的作用域中有objectId:

<a ui-sref="statename({'id': objectId})">Change states</a>
然后,您可以使用
$stateParams
在解析中检索该参数:

resolve: {
    contactService: 'contactService',
    contacts: function ($stateParams, contactService) {
        return contactService.getContacts($stateParams.id);
    }
}

作为可接受解决方案的替代方案,该解决方案要求为同一资源(如果您从服务器/api获取值)再次往返到服务器,您可以从子控制器
$watch
父控制器

function ParentController($http) {
  var vm = this;
  $http.get(someResourceUrl).then(function(res) {
    vm.someResource = res.data;
  });
}

function ChildController($scope) {
  // wait untill the parent gets the value
  var unwatch = $scope.$watch('parent.someResource', function(newValue) {
    if (newValue) {
      // the parent has the value, init this controller
      init(newValue);
      // dispose of the watcher we no longer need
      unwatch();
    }
  });
  function init(someResource) {
    // ... do something
  }
}

function routerConfig($stateProvider) {
  $stateProvider
    .state('parent', {
      url: '/parent',
      controller: 'ParentController',
      controllerAs: 'parent',
      templateUrl: '...',
    })
    .state('parent.child', {
      url: '/child',
      controller: 'ChildController',
      controllerAs: 'child',
      templateUrl: '...',
    });
}

你能在plnkr中发布你的代码吗?我不认为我的问题需要更多的解释代码,我只是想在我的ui路由器的解析中添加$scope。你想做的是不可能的。如果您需要一个值来执行resolve函数中的计算,则该值必须来自服务。由于您需要父$scope变量,我怀疑您可以使用某种参数导航到此状态。这就是做到这一点的方法。或者您可以从父状态继承并重用父状态的参数。确切地说,但是已经有实例化的作用域和控制器。例如,如果我没有客户端ID,我应该如何解析客户端数据?您应该将其作为参数传递到下一个状态,例如:
$state.go('name'.{ID:1})
ui sref=“name({ID:1})”
然后注入
$stateParams
在您的决心中再次检索它并根据它获取数据现在我们正在取得进展,感谢您的回答。阅读您的问题,而不是您真正想做的,应该在我的回答中添加这一点,很抱歉,现在为未来的用户这样做了。
function ParentController($http) {
  var vm = this;
  $http.get(someResourceUrl).then(function(res) {
    vm.someResource = res.data;
  });
}

function ChildController($scope) {
  // wait untill the parent gets the value
  var unwatch = $scope.$watch('parent.someResource', function(newValue) {
    if (newValue) {
      // the parent has the value, init this controller
      init(newValue);
      // dispose of the watcher we no longer need
      unwatch();
    }
  });
  function init(someResource) {
    // ... do something
  }
}

function routerConfig($stateProvider) {
  $stateProvider
    .state('parent', {
      url: '/parent',
      controller: 'ParentController',
      controllerAs: 'parent',
      templateUrl: '...',
    })
    .state('parent.child', {
      url: '/child',
      controller: 'ChildController',
      controllerAs: 'child',
      templateUrl: '...',
    });
}