Main.js
$routeProvider
.when('/home', {
templateUrl: 'home.html',
controller: 'StudentController'
})
.when('/viewStudents', {
templateUrl: 'viewStudents.html',
controller: 'StudentController'
})
.when('/viewTeacher', {
templateUrl: 'viewTeacher.html',
controller: 'StudentController'
})
.otherwise({
redirectTo: '/home'
});
另一个js中的代码
$rootScope.$on("$routeChangeStart", function(event, next, current){
console.log(event);
//here i want to detect
});
当用户访问index.html home路由时,JS触发并添加到 View 中的home.html
.otherwise({
redirectTo: '/home'
});
有一个按钮调用 viewStudents.html 然后路由会改变并且 viewStudents.html 将呈现
我如何在routeChangeStart函数中获取由于js或用户点击而导致路线发生变化的信息
最佳答案
由“$rootScope.$emit”或$rootScope.$broadcast触发的routeChangeStart事件
此事件系统的用途是将数据子 Controller 传递给父 Controller 。
当您调用广播事件时,$on 事件将调用。
当你的 Controller 加载时,它会被调用一次。您可以使用函数在外部调用它。
app.controller('ChildCtrl',
function ChildCtrl ($rootScope) {
$rootScope.$emit('rootScope:emit', 'Emit!'); // $rootScope.$on
$rootScope.$broadcast('rootScope:broadcast', 'Broadcast'); // $rootScope.$on && $scope.$on
});
app.controller('ParentCtrl',
function SiblingOneCtrl ($rootScope) {
var myListener = $scope.$on('child', function (event, data) {
// do something
});
});
app.controller('ParentCtrl',
function ParentCtrl ($scope) {
var myListener = $rootScope.$on('child', function (event, data) {
// do something
});
});
关于javascript - 我将如何了解JS触发的routeChangeStart事件或点击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41937707/