• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • AngularJS: passing param btw pages

    Blogs20142014-12-15


    AngularJS: parse param from 1 page, and utilize the param in another page’s click event

    I am not quite sure the following codes work or not:

    //1. use $on and $emit to pass param between pages:
    $rootScope.$on('click-different-links-finally-get-last-same-page',
      function(from_url) {
        $scope.from_url = from_url;
    });
    $rootScope.$emit('click-different-links-finally-get-last-same-page',
      '/router-urls');
    
    
    //2. in the last page, define the click event to return previous page
    // whichever it comes from:
    angular.module('testApi', [])
     .directive('testDirective', function() {
      link: function(scope, element, attrs) {
        element.click(function($event) {
          $event.preventDefault();
          attrs.$observe('from_url', function(value) {
            $timeout(function() {
              scope.$apply(function() {
                $location.path(from_url);
              })}, 1);
          });
        });
      }
    });

    Will test to verify it.