简体   繁体   English

如何从另一个ng-controller访问另一个ng-controller ng-model?

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

I want to access the ng-model from another ng-controller is it possible and how?. 我想从另一个ng-controller访问ng-model,这可能吗?如何? In the following code I am using two controller, first controller having mddl1 and another one controller don't have any other model. 在下面的代码中,我使用两个控制器,第一个具有mddl1的控制器,另一个没有任何其他模型。 But I want to access the model value from 2nd controller method. 但是我想从第二个控制器方法访问模型值。

<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>

I am tried to access the ng-model value from another ng-controller value like following code. 我试图从另一个ng-controller值访问ng-model值,例如以下代码。

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

In common, use service . 通常,使用service When parentCtrl send data to childCtrl , can use $broadcast $on .Conversely, can use $emit $on . 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 {

   }
  });`

You can find best practice code at here . 您可以在此处找到最佳实践代码。 This will become best solution for you. 这将成为您的最佳解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM