在属于某个路径的模板上,我想点击调用范围中方法的链接或按钮,然后从那里转换到新路径并在模板上查找新路径的元素。基本上是这样的:
app.controller('SomeController', function SomeController($scope, $location) {
$scope.goToOverview = function (showModal) {
$location.path("overview/basic");
if(showModal){
$('#basicModal').modal('toggle');
}
};
});
问题是当我到达jQuery选择器时,尚未加载“overview / basic”路由的模板,因此找不到该元素。
如何解决这个问题?
谢谢。
答案 0 :(得分:0)
作为requested,here是从一个控制器广播事件并将其捕获到另一个控制器中的示例:
<div ng-app="MyApp">
<div ng-controller="OneCtrl">
<form>
<input type="text" ng-model="msg"></input>
<input type="submit" ng-click="send(msg)" value="Broadcast"></input>
</form>
</div>
<div ng-controller="TwoCtrl">
<p>Message from OneCtrl: {{ msg }}</p>
</div>
</div>
<script type="text/javascript">
var app = angular.module("MyApp", []);
app.controller('OneCtrl', function ($scope, $rootScope) {
$scope.send = function (msg) {
$rootScope.$broadcast('one_msg', {
msg: msg
});
};
});
app.controller('TwoCtrl', function ($scope) {
$scope.$on('one_msg', function (event, data) {
$scope.msg = data.msg;
});
});
</script>