簡體   English   中英

如何從另一個ng-controller訪問另一個ng-controller ng-model?

[英]How can I access the another ng-controller ng-model from another ng-controller?

我想從另一個ng-controller訪問ng-model,這可能嗎?如何? 在下面的代碼中,我使用兩個控制器,第一個具有mddl1的控制器,另一個沒有任何其他模型。 但是我想從第二個控制器方法訪問模型值。

<div ng-controller="cnt_fnt">
<select id="dflfnt" ng-model='mddl1' ng-options='option.name for option in ddl1options.data'> 
</select>
</div>
<div ng-controller="ctrl_opsection" ng-model="opmodel">
</div>
<script>
----
.controller('cnt_fnt', ['$scope', function ($scope) {
$scope.ddl1options={
data:[
{value:"Georgia, serif",name:"Style 1"},
{value:"'Times New Roman', Times, serif",name:"Style 3"},
{value:"Arial, Helvetica, sans-serif",name:"Style 4"}
]};
  alert($scope.mddl1.value); //Here I Can Access the dropdown value
}])
.controller("ctrl_opsection",["$scope",function($scope){
    alert(cnt_fnt.mddl1.value); //I want to access here that dropdown value 
}]);
</script>

我試圖從另一個ng-controller值訪問ng-model值,例如以下代碼。

alert(cnt_fnt.mddl1.value); //controller_name.modelname

通常,使用service parentCtrl將數據發送到childCtrl ,可以使用$broadcast $on相反,可以使用$emit $on

 `controller('cnt_fnt', ['$scope','inter', function($scope,inter){
    inter.data = $scope.mddl1.value;
  }])
  .controller('dockCtr', ['$scope', 'inter', function($scope, inter){
     alert(inter.data)
  }]);

  app.service('inter', function(){
   return {

   }
  });`

您可以在此處找到最佳實踐代碼。 這將成為您的最佳解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM