Javascript 无法访问控制器和视图中的$scope变量-AngularJS

Javascript 无法访问控制器和视图中的$scope变量-AngularJS,javascript,angularjs,Javascript,Angularjs,我必须绑定控制器(GridSearchCtrl)中的几个下拉列表,即另一个控制器(GridCtrl)。我正在尝试下面的代码,但它给了我错误 $scope.advSearch未定义 DiplomaProgramController.controller('GridSearchCtrl', ['$scope', 'DiplomaProgramService', function ($scope, DiplomaProgramService) { $scope.lo

我必须绑定控制器(GridSearchCtrl)中的几个下拉列表,即另一个控制器(GridCtrl)。我正在尝试下面的代码,但它给了我错误 $scope.advSearch未定义

  DiplomaProgramController.controller('GridSearchCtrl', ['$scope', 'DiplomaProgramService', 
      function ($scope, DiplomaProgramService) {

        $scope.loadDropDowns = function ($scope) {
          DiplomaProgramService.loadDropDowns('abcd').then(function (DiplomaProgram) {
            $scope.advSearch.depts = DiplomaProgram.depts;
            $scope.advSearch.degreesList = DiplomaProgram.degreesList;
            // Here it shows the list perfectly
            console.log($scope.advSearch.degreesList);
          });
       };
       $scope.loadDropDowns($scope);
       // Here it generates error i.e. $scope.advSearch is undefined
       console.log($scope.advSearch.degreesList);
}]);
景色是这样的

    <select id="ddlDegrees" name="degrees" ng-model="advSearch.DegreeLevelID" ng-options="a.DegreeLevelID as a.DegreeLevelName for a in degreesList" required>
        <option value="">-- select --</option>
    </select>

--挑选--

我猜.then(函数)中的内容是异步的,因此在定义$scope.advSearch之前执行console.log

尝试这样做(我真的不知道承诺是如何工作的,所以$scope.$apply在这种情况下可能根本不相关):

它仍然不能与console.log一起工作,但它应该告诉Angular您的范围已经更新,并使您的视图工作

DiplomaProgramService.loadDropDowns('abcd').then(function (DiplomaProgram) {
    $scope.$apply(function() {
        $scope.advSearch.depts = DiplomaProgram.depts;
        $scope.advSearch.degreesList = DiplomaProgram.degreesList;
        // Here it shows the list perfectly
        console.log($scope.advSearch.degreesList);
    }
});