2017-01-30 44 views
0

Main.js我将如何得到由JS触发routeChangeStart事件事件知道或单击

$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的回家路线从JS解雇& home.html做为鉴于

.otherwise({ 
      redirectTo: '/home' 
     }); 

添加有按钮哪个呼叫viewStudents.html然后击溃Ë将发生变化,viewStudents.html将呈现

我将如何在该路线由于更改为JS routeChangeStart功能获得或按用户

+0

你可以尝试检查event.type。 Idk如果角度传播。 – Lacrioque

回答

2
通过任一触发

routeChangeStart事件“$ rootScope。$发出”或$ rootScope点击$ broadcast

使用此事件系统是将数据子控制器传递给父级。

当您调用广播事件时,$ on事件将会调用。

当您的控制器加载时调用一次。你可以使用函数在外部调用它。

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 
     }); 
});