是否有可能让一个控制器使用另一个?
例如:
此HTML文档只是打印MessageCtrl控制器在messageCtrl.js文件中传递的消息.
<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>
Run Code Online (Sandbox Code Playgroud)
控制器文件包含以下代码:
function MessageCtrl()
{
this.message = function() {
return "The current date is: " + new Date().toString();
};
}
Run Code Online (Sandbox Code Playgroud)
这只是打印当前日期;
如果我要添加另一个控制器,DateCtrl它将日期以特定格式交还给MessageCtrl,那怎么会这样做呢?DI框架似乎关注XmlHttpRequests和访问服务.
我需要在角度j的另一个控制器中调用函数.如果有可能,请提前帮助我
代码:
app.controller('One', ['$scope',
function($scope) {
$scope.parentmethod = function() {
// task
}
}
]);
app.controller('two', ['$scope',
function($scope) {
$scope.childmethod = function() {
// Here i want to call parentmethod of One controller
}
}
]);
Run Code Online (Sandbox Code Playgroud)