问题:
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
来源:oschina
链接:https://my.oschina.net/u/4438370/blog/4279060