2016-04-27 84 views
0

我在父控制器的http.get()方法内定义了$ scope变量。我不能直接从孩子控制器访问它,因为它说未定义。当我使用Watch函数从我的子控制器访问这些范围变量时,它的工作正常。但是,对于更多数量的范围变量我该如何使用手表功能?或者,另一种方法可以在不使用手表的情况下做到这一点? 我已经更新了我的小提琴这里click here fiddle for accessing more number of parent scope variables使用手表从子控制器访问父范围

function ParentCtrl($scope, $http) { 
 
     $scope.cities = ["NY", "Amsterdam", "Barcelona"]; 
 
     
 
     // $scope.example1 = $http.get('/echo/json'); 
 
     $http.get('/echo/json').then(function(value) { 
 
    \t  $scope.example2 = value.status; 
 
    \t \t }); 
 
     $http.get('/echo/json/error').then(null,function(value) { 
 
     $scope.example3 = value.status; 
 
     }); 
 
     $http.get('/echo/json').success(function(data, status, headers, config) { 
 
     $scope.example4 = status; 
 
     }); 
 
     $http.get('/echo/json/error').error(function(data, status, headers, config) { 
 
     $scope.example5 = status; 
 
     });   
 
     $http.get('/echo/json/error').catch(function(value) { 
 
     $scope.example6 = value.status; 
 
     });   
 
     
 
     } 
 
     
 
    function ChildCtrl($scope) { 
 
     $scope.parentcities = $scope.$parent.cities; 
 
    // console.log("1=="+$scope.example1);//undefined 
 
     console.log("2=="+$scope.example2);//undefined 
 
     console.log("3=="+$scope.example3);//undefined 
 
     console.log("4=="+$scope.example4);//undefined 
 
     console.log("5=="+$scope.example5);//undefined 
 
     console.log("6=="+$scope.example6);//undefined 
 
     
 
     
 
     
 
     /*$scope.$watch("example1", function(example1) { 
 
     if(angular.isDefined(example1)){ 
 
     console.log("1_watch=="+$scope.example1); //Defined 
 
     } 
 
     });*/ 
 
     $scope.$watch("example2", function(example2) { 
 
     if(angular.isDefined(example2)){ 
 
     console.log("2_watch=="+$scope.example2); //Defined 
 
     } 
 
     }); 
 
     $scope.$watch("example3", function(example3) { 
 
     if(angular.isDefined(example3)){ 
 
     console.log("3_watch=="+$scope.example3); //Defined 
 
     } 
 
     }); 
 
     $scope.$watch("example4", function(example4) { 
 
     if(angular.isDefined(example4)){ 
 
     console.log("4_watch=="+$scope.example4); //Defined 
 
     } 
 
     }); 
 
     $scope.$watch("example5", function(example5) { 
 
     if(angular.isDefined(example5)){ 
 
     console.log("5_watch=="+$scope.example5); //Defined 
 
     } 
 
     }); 
 
     $scope.$watch("example6", function(example6) { 
 
     if(angular.isDefined(example6)){ 
 
     console.log("6_watch=="+$scope.example6); //Defined 
 
     } 
 
     }); 
 
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script> 
 
    <div ng-app ng-controller="ParentCtrl"> 
 
     <div ng-controller="ChildCtrl as vm"> 
 
      {{$parent.cities}} 
 
     </div> 
 
    </div>

+0

你的小提琴链接不是超链接。你可以编辑 – Maverick

+0

@Maverick你可以检查现在plz与点击这里链接 – VVijay

回答

0

你不应该使用的手表和$双亲在这种情况下。

父控制器可以使用事件($ broadcast)来通知子控制器该值已检索并可以使用。

$scope.$broadcast("example1",$scope.example1) //parent controller 

$scope.$on("example1",function(data){ 
    // Do stuff 
}) //child controller 

我的首选方法是将数据存储在工厂中,如果它必须跨控制器共享。

如果您仍然需要$ watch出于某种原因,您可以在检索数据后终止该手表,以使其不再运行。这可以通过使用$ watch()返回的回调来完成。

var killExample4 = $scope.$watch("example4", function(example4) { 
    if(angular.isDefined(example4)){ 
    console.log("4_watch=="+$scope.example4); //Defined 
    killExample4(); 
    } 
}); 
+0

我怎样才能以这种方式访问​​更多数量的范围变量??? var killExample4 = $ scope。$ watch(“example4”,function(example4){ if(angular.isDefined(example4)){ console.log(“4_watch ==”+ $ scope.example4); // Defined killExample4(); } }); var killExample3 = $ scope。$ watch(“example3”,function(example3){ if(angular.isDefined(example3)){ console.log(“3_watch ==”+ $ scope.example3); // Defined killExample3(); } }); .....等等这是正确的方式? – VVijay

+0

您可以保留变量在工厂并注入到两个控制器中。 – Maverick

+0

k将尝试使用角度服务 – VVijay