Search code examples
angularjsangularjs-directiveangularjs-ng-click

linking angular ng-click to open new page and passing param:


Here when user clicks edit button, I need to open a new page and also make rest call to populate that page with the param passed in.


Solution

  • If I understand your question, you want to know how to set up a link that takes you to a new page with access to the params you're sending. There are a few ways, depending on what modules you're using.

    I recommend working with ui.router, but if you aren't using that, you can check out how to do it with ngRoute here.

    If you're using ui-router, you can do this:

    <a data-ui-sref="newPage(employee)">Edit Employee</a>
    

    When the user clicks the link, it will take you to newPage, with the param employee.

    In your newPage controller, you can include $state as a param, and then access this employee object.

    app.controller('newPageController', ['$state', '$http', function ($state, $http) {
    
        var employeeData = $state.params.employee;
    
        //Make REST calls here
        $http({
          url: 'REST url',
          type: 'POST',
          data: employeeData
        }).then(function (res) {
           //success, etc
        });
    
    
    }]);
    

    Also, if you're new to Angular, I recommend the tutorial on their site. It covers how to do routing.