一个AngularJS控制器可以调用另一个吗?

孤街浪徒 提交于 2020-08-11 19:37:46

问题:

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文档仅在messageCtrl.js文件中打印由MessageCtrl控制器传递的MessageCtrl

<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? 如果要添加另一个控制器DateCtrl ,它将特定格式的日期交还给MessageCtrl ,那该怎么办呢? The DI framework seems to be concerned with XmlHttpRequests and accessing services. DI框架似乎与XmlHttpRequests和访问服务有关。


解决方案:

参考一: https://stackoom.com/question/czdv/一个AngularJS控制器可以调用另一个吗
参考二: https://oldbug.net/q/czdv/Can-one-AngularJS-controller-call-another
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!