Angularjs 访问控制器外部的ng模型数据

Angularjs 访问控制器外部的ng模型数据,angularjs,angularjs-ng-model,Angularjs,Angularjs Ng Model,我已经写了下面的代码 <span ng-controller="calanderCtrl"> <input type="text" ng-model="onDate"> </span> <pre>user.name = <span ng-bind="onDate"></span></pre> user.name= 我知道它在ng控制器之外,因此无法绑定数据,但我的应用程序需要calanderCtrl控制

我已经写了下面的代码

<span ng-controller="calanderCtrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-bind="onDate"></span></pre>

user.name=

我知道它在ng控制器之外,因此无法绑定数据,但我的应用程序需要calanderCtrl控制器。我想把这个值放到作用域中,这样我也可以在其他控制器中使用它。如何执行此操作?

您可以为此使用发布-订阅模式。这样可以避免将变量放在rootscope上

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
加载模板时,将调用控制器

rootscope
    scope1 ---> subscribes to the emit $scope.$on
      scope2 ---> performs a $scope.$emit
        scope3 ---> subscribes to the emit $scope.$on
只有scope1将捕获该事件。因为$scope.$emit位于链的上游。 这是一种只更新特定作用域的方法。虽然大部分的工作都是这样的

我们在scope1和scope3的控制器中注入$rootScope,并在rootScope上订阅emit。因为rootscope是最高的作用域,所以它总是从scope2捕获$emit。这是一种仅将事件发送到在rootscope上订阅的特定控制器的方法

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
最后,我们也可以这样做:


我们现在在根示波器上大喊大叫,而不是像emit一样向上移动,而是向下传播。这相当于在房间里大喊大叫,每个没有戴护耳器的人都会听到。实际上,每个在本地范围订阅广播正在发送的事件的人都可以使用发布-订阅模式。这样可以避免将变量放在rootscope上

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
加载模板时,将调用控制器

rootscope
    scope1 ---> subscribes to the emit $scope.$on
      scope2 ---> performs a $scope.$emit
        scope3 ---> subscribes to the emit $scope.$on
只有scope1将捕获该事件。因为$scope.$emit位于链的上游。 这是一种只更新特定作用域的方法。虽然大部分的工作都是这样的

我们在scope1和scope3的控制器中注入$rootScope,并在rootScope上订阅emit。因为rootscope是最高的作用域,所以它总是从scope2捕获$emit。这是一种仅将事件发送到在rootscope上订阅的特定控制器的方法

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
最后,我们也可以这样做:


我们现在在根示波器上大喊大叫,而不是像emit一样向上移动,而是向下传播。这相当于在房间里大喊大叫,每个没有戴护耳器的人都会听到。实际上,每个在本地范围订阅广播正在发送的事件的人都可以使用发布-订阅模式。这样可以避免将变量放在rootscope上

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
加载模板时,将调用控制器

rootscope
    scope1 ---> subscribes to the emit $scope.$on
      scope2 ---> performs a $scope.$emit
        scope3 ---> subscribes to the emit $scope.$on
只有scope1将捕获该事件。因为$scope.$emit位于链的上游。 这是一种只更新特定作用域的方法。虽然大部分的工作都是这样的

我们在scope1和scope3的控制器中注入$rootScope,并在rootScope上订阅emit。因为rootscope是最高的作用域,所以它总是从scope2捕获$emit。这是一种仅将事件发送到在rootscope上订阅的特定控制器的方法

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
最后,我们也可以这样做:


我们现在在根示波器上大喊大叫,而不是像emit一样向上移动,而是向下传播。这相当于在房间里大喊大叫,每个没有戴护耳器的人都会听到。实际上,每个在本地范围订阅广播正在发送的事件的人都可以使用发布-订阅模式。这样可以避免将变量放在rootscope上

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
加载模板时,将调用控制器

rootscope
    scope1 ---> subscribes to the emit $scope.$on
      scope2 ---> performs a $scope.$emit
        scope3 ---> subscribes to the emit $scope.$on
只有scope1将捕获该事件。因为$scope.$emit位于链的上游。 这是一种只更新特定作用域的方法。虽然大部分的工作都是这样的

我们在scope1和scope3的控制器中注入$rootScope,并在rootScope上订阅emit。因为rootscope是最高的作用域,所以它总是从scope2捕获$emit。这是一种仅将事件发送到在rootscope上订阅的特定控制器的方法

<span ng-controller="Ctrl">
<input type="text" ng-model="onDate">
</span>  
<pre>user.name = <span ng-controller="Ctrl2" ng-bind="onDate"></span></pre>
最后,我们也可以这样做:



我们现在在根示波器上大喊大叫,而不是像emit一样向上移动,而是向下传播。这相当于在房间里大喊大叫,每个没有戴护耳器的人都会听到。本质上,每个在本地范围订阅广播发送的事件的人都使用$rootscopevariable@Sajeetharan-你能给我看看示例代码使用$rootscope吗variable@Sajeetharan-你能给我看看示例代码使用$rootscope吗variable@Sajeetharan-你能给我看看示例代码使用$rootscope吗variable@Sajeetharan-你能给我看看吗me示例代码Ctrl1和Ctrl2何时被调用?$Ctrl2未被调用invoked@Hacker你能给我看看你的模板吗。还要确保将控制器注册到angular,并将js文件添加到index.htmlfile@Amo_Geismar-对不起,我弄错了,它起作用了!!谢谢但是1个控制器中的作用域如何与另一个控制器共享??你能解释一下它是怎么工作的吗!!我只是不想盲目的去做。@Hacker我实际上犯了一个错误,我在前面的例子中犯了这个错误。我更新了我的答案,以便更好地解释发生了什么。种类RegardsCTRL1和Ctrl2何时被调用?$Ctrl2未被调用invoked@Hacker你能给我看看你的模板吗。还要确保将控制器注册到angular,并将js文件添加到index.htmlfile@Amo_Geismar-对不起,我弄错了,它起作用了!!谢谢但是1个控制器中的作用域如何与另一个控制器共享??你能解释一下它是怎么工作的吗!!我只是不想盲目的去做。@Hacker我实际上犯了一个错误,我在前面的例子中犯了这个错误。我更新了我的答案,以便更好地解释发生了什么。种类RegardsCTRL1和Ctrl2何时被调用?$Ctrl2未被调用invoked@Hacker你能给我看看你的模板吗。还要确保将控制器注册到angular,并将js文件添加到index.htmlfile@Amo_Geismar-对不起,我弄错了,它起作用了!!谢谢但是1个控制器中的作用域如何与另一个控制器共享??你能解释一下它是怎么工作的吗!!我只是不想做