一个 AngularJS 控制器可以调用另一个吗? [英] Can one AngularJS controller call another?

查看:35
本文介绍了一个 AngularJS 控制器可以调用另一个吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以让一个控制器使用另一个控制器?

例如:

这个 HTML 文档只是在 messageCtrl.js 文件中打印由 MessageCtrl 控制器传递的消息.

<头><meta charset="utf-8"/><title>控制器间通信</title><身体><div ng:controller="MessageCtrl"><p>{{message}}</p>

<!-- Angular 脚本 --><script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script><script src="js/messageCtrl.js" type="text/javascript"></script></html>

控制器文件包含以下代码:

function MessageCtrl(){this.message = function() {return "当前日期为:" + new Date().toString();};}

它只是打印当前日期;

如果我要添加另一个控制器,DateCtrl,它将以特定格式的日期返回给 MessageCtrl,人们会怎么做呢?DI 框架似乎与 XmlHttpRequests 和访问服务有关.

解决方案

控制器之间的通信有多种方式.

最好的可能是共享服务:

function FirstController(someDataService){//使用数据服务,绑定到模板...//或调用 someDataService 上的方法向服务器发送请求}功能 SecondController(someDataService){//引用了相同的服务实例//例如,如果服务更新状态,则该控制器知道它}

另一种方法是在作用域上发出事件:

function FirstController($scope){$scope.$on('someEvent', function(event, args) {});//另一个控制器甚至指令}功能 SecondController($scope){$scope.$emit('someEvent', args);}

在这两种情况下,您也可以与任何指令进行通信.

Is it possible to have one controller use another?

For example:

This HTML document simply prints a message delivered by the MessageCtrl controller in the messageCtrl.js file.

<html xmlns:ng="http://angularjs.org/">
<head>
    <meta charset="utf-8" />
    <title>Inter Controller Communication</title>
</head>
<body>
    <div ng:controller="MessageCtrl">
        <p>{{message}}</p>
    </div>

    <!-- Angular Scripts -->
    <script src="http://code.angularjs.org/angular-0.9.19.js" ng:autobind></script>
    <script src="js/messageCtrl.js" type="text/javascript"></script>
</body>
</html>

The controller file contains the following code:

function MessageCtrl()
{
    this.message = function() { 
        return "The current date is: " + new Date().toString(); 
    };
}

Which simply prints the current date;

If I were to add another controller, DateCtrl which handed the date in a specific format back to MessageCtrl, how would one go about doing this? The DI framework seems to be concerned with XmlHttpRequests and accessing services.

解决方案

There are multiple ways how to communicate between controllers.

The best one is probably sharing a service:

function FirstController(someDataService) 
{
  // use the data service, bind to template...
  // or call methods on someDataService to send a request to server
}

function SecondController(someDataService) 
{
  // has a reference to the same instance of the service
  // so if the service updates state for example, this controller knows about it
}

Another way is emitting an event on scope:

function FirstController($scope) 
{
  $scope.$on('someEvent', function(event, args) {});
  // another controller or even directive
}

function SecondController($scope) 
{
  $scope.$emit('someEvent', args);
}

In both cases, you can communicate with any directive as well.

这篇关于一个 AngularJS 控制器可以调用另一个吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆