簡體   English   中英

我如何在JS觸發的routeChangeStart事件事件中了解或單擊

[英]How i will get to know in routeChangeStart event event triggered by JS or click

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

在視圖中添加了用戶訪問從JS和home.html觸發的index.html主頁路線時

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

有一個調用viewStudents.html的按鈕,然后路由將更改,並且viewStudents.html將被呈現

我將如何在routeChangeStart函數中得知路由由於jS或用戶單擊而發生更改

由$ rootScope。$ emit或$ rootScope。$ broadcast觸發的routeChangeStart事件

此事件系統的用途是將數據子控制器傳遞給父控制器。

當您調用廣播事件時,將調用$ 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
      });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM