Why the controller logic is executed only if you are in another view ?
为什么只有在另一个视图中才执行控制器逻辑?
http://jsfiddle.net/J5hRc/128/
If you press twice in a link, the alert is only showed the first time. I am doing something wrong ?
如果您在链接中按两次,则仅在第一次显示警报。我做错了什么?
HTML:
<script type="text/ng-template" id="page1.html">
Page 1
</script>
<script type="text/ng-template" id="page2.html">
Page 2
</script>
<div>
<ul>
<li><a href="#/link1">Page 1</a></li>
<li><a href="#/link2">Page 2</a></li>
</ul>
<div ng-view></div>
</div>
Javascript:
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl1', function($scope) {
alert('controller logic1'); });
myApp.controller('MyCtrl2', function($scope) {
alert('controller logic2'); });
myApp.config(function($routeProvider) {
$routeProvider
.when('/link1', {
controller: 'MyCtrl1',
templateUrl: 'page1.html'
})
.when('/link2', {
controller: 'MyCtrl2',
templateUrl: 'page2.html'
});
});
3 个解决方案
#1
1
Your alerts are in the constructor function of the controller. So, they are obviously only going to fire when the controller is created.
您的警报位于控制器的构造函数中。因此,它们显然只会在创建控制器时触发。
The controllers in your example are created when the route is changed, so that is why you see them fire the alert when you change to a new route. Once you're on a route, the controller won't be instantiated again unless you leave and come back.
路由更改时会创建示例中的控制器,因此当您更改为新路由时,您会看到它们触发警报。一旦你在路线上,除非你离开并回来,否则控制器将不再被实例化。
If you want this logic to fire every time you click the link as well as when you first navigate to the view, then wire up a clickHandler and call the clickHandler in the constructor of the controller:
如果您希望每次单击链接时以及首次导航到视图时触发此逻辑,则连接clickHandler并在控制器的构造函数中调用clickHandler:
myApp.controller('MyCtrl1', function($scope) {
$scope.showAlert = function () {
alert('controller logic1');
}
$scope.showAlert();
});
And in your view:
在你看来:
<li><a ng-click="showAlert()" href="#/link1">Page 1</a></li>
#2
1
No. You're not doing anything wrong. This is the default behavior in the web and you browser makes sure it doesn't navigate to a link that you're already on, common sense if you think about it. You can reload the route if you manually navigate to it and call $route.reload()
(instead of using hrefs). For example:
不,你没有做错任何事。这是网络中的默认行为,您的浏览器会确保它不会导航到您已经使用过的链接,如果您考虑它就会有常识。如果手动导航到路径并调用$ route.reload()(而不是使用hrefs),则可以重新加载路径。例如:
$scope.goToRoute= function(url){
$location.path(url);
$route.reload();
}
#3
0
Because if you are on the page1 with $location path /link1
, and then you click on the link /link1
location path does not change again of course, hence route won't change either. And because of that $routeChangeSuccess
event never occurs, which is being listened by ngView
directive. This ngView
directive internal event listener responsible for controller instantiating, which of course won't happen too.
因为如果你在page1上有$ location path / link1,那么你点击链接/ link1位置路径当然不会再改变,因此路由也不会改变。因为$ routeChangeSuccess事件永远不会发生,这是由ngView指令监听的。这个ngView指令内部事件监听器负责控制器实例化,当然也不会发生这种情况。
#1
1
Your alerts are in the constructor function of the controller. So, they are obviously only going to fire when the controller is created.
您的警报位于控制器的构造函数中。因此,它们显然只会在创建控制器时触发。
The controllers in your example are created when the route is changed, so that is why you see them fire the alert when you change to a new route. Once you're on a route, the controller won't be instantiated again unless you leave and come back.
路由更改时会创建示例中的控制器,因此当您更改为新路由时,您会看到它们触发警报。一旦你在路线上,除非你离开并回来,否则控制器将不再被实例化。
If you want this logic to fire every time you click the link as well as when you first navigate to the view, then wire up a clickHandler and call the clickHandler in the constructor of the controller:
如果您希望每次单击链接时以及首次导航到视图时触发此逻辑,则连接clickHandler并在控制器的构造函数中调用clickHandler:
myApp.controller('MyCtrl1', function($scope) {
$scope.showAlert = function () {
alert('controller logic1');
}
$scope.showAlert();
});
And in your view:
在你看来:
<li><a ng-click="showAlert()" href="#/link1">Page 1</a></li>
#2
1
No. You're not doing anything wrong. This is the default behavior in the web and you browser makes sure it doesn't navigate to a link that you're already on, common sense if you think about it. You can reload the route if you manually navigate to it and call $route.reload()
(instead of using hrefs). For example:
不,你没有做错任何事。这是网络中的默认行为,您的浏览器会确保它不会导航到您已经使用过的链接,如果您考虑它就会有常识。如果手动导航到路径并调用$ route.reload()(而不是使用hrefs),则可以重新加载路径。例如:
$scope.goToRoute= function(url){
$location.path(url);
$route.reload();
}
#3
0
Because if you are on the page1 with $location path /link1
, and then you click on the link /link1
location path does not change again of course, hence route won't change either. And because of that $routeChangeSuccess
event never occurs, which is being listened by ngView
directive. This ngView
directive internal event listener responsible for controller instantiating, which of course won't happen too.
因为如果你在page1上有$ location path / link1,那么你点击链接/ link1位置路径当然不会再改变,因此路由也不会改变。因为$ routeChangeSuccess事件永远不会发生,这是由ngView指令监听的。这个ngView指令内部事件监听器负责控制器实例化,当然也不会发生这种情况。