简体   繁体   中英

Angular.js - How to pass data from controller to view

This is the first time i am using Angular.js. So my workflow could be wrong.

How do i pass data from controller to the view

  1. ng-view -> Displays html page using jade
  2. When user clicks on submit button, i use $http on the controller and submit the request to the server.
  3. The server returns me the necessary data back which i need to pass to another view.

My code snippet

function TrackController($scope,$http,$location,MessageFactory){
  $scope.message = MessageFactory.contactMessage();

  $scope.submit = function () {
    var FormData = {
        'track_applicationid': $scope.track_applicationid,
        'track_email': $scope.track_email
    }
    $http({method: 'POST', url: '/track', data: FormData}).
      success(function(data, status, headers, config) {
        $scope.registeredDate = 'data.REGISTERED_DATE';
        $scope.filedDate = data.FILED_DATE;
        $location.path('trackMessage');
      }).
      error(function(data, status, headers, config) {
        console.log('error');
      });   
}
}

In the above code, i want to pass registeredDate and filedDate to trackMessage view.

After going through the comments, i understood you are using one controller for two views. If you want to set values to $scope.registeredDate and $scope.filedDate , You have to declare those objects globally using root-scope(Not recommended) or use Angular values.

I recommended to use two different controllers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM