2016-04-27 18 views
0

親コントローラのhttp.get()メソッド内に$ scope変数を定義しました。私は子供のコントローラから直接アクセスすることはできません。ウォッチ機能を使用して子コントローラからスコープ変数にアクセスしたときに正常に機能しています。スコープ変数の数を増やすにはどうすればよいですか?それとも時計を使用せずにこれを行う別の方法ですか? 私はあなたがこのような場合には、時計と$親を使用すべきではない、ここで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

私のバイオリン更新しました。

イベント($ブロードキャスト)を親コントローラで使用して、値が取得され使用の準備ができたことを子コントローラに通知できます。

$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

この方法でより多くのスコープ変数にアクセスするにはどうすればいいですか? { } { console.log( "4_watch ==" + $ scope.example4); //定義済み {0} {0} killExample4(); } }); { } { console.log( "3_watch ==" + $ scope.example3); //定義済み {0} {0} {0} killExample3(); } }); .....これは正しい方法ですか? – VVijay

+0

変数を工場出荷時のままにして、コントローラの両方に注入することができます – Maverick

+0

kは角度サービスで試行します – VVijay

関連する問題