简体   繁体   中英

How to pass some data from View to controller in angularJs

 `<tr ng-repeat="x in rest">
   <td>{{ x.groupId }}</td>
    <td><a  href="#/offers">{{ x.groupName }}</a></td>
  </tr>`

this is the code of my view. I want to pass the group id back to controller so that i can run another API which uses this groupId to fetch contacts. I tried Using cookies for this but i receive an error

`Uncaught ReferenceError: $cookies is not defined` at my controller at `line:1`

Passing data to controller is very simple.You can do it as below-:

<tr ng-repeat="x in rest">
   <td>{{ x.groupId }}</td>
   <td><a  href="#/offers/{{ x.groupId }}">{{ x.groupName }}</a></td>
</tr>

RouteConfig:

  when('/offers/:groupId', {
        templateUrl: 'ViewName.html',
        controller: 'CtrlName'
      }).

Controller code:

 TestControllers.controller('CtrlName', ['$scope', '$routeParams',
      function($scope, $routeParams) {
        $scope.groupId = $routeParams.groupId ;
      }]);

For reference you can check -: https://docs.angularjs.org/tutorial/step_07

Hope this helps.

you can pass data from views to controller using

ngModel


//sample Views
<form name="testForm" ng-controller="ExampleController">
    <input ng-model="val" ng-pattern="/^\d+$/" name="anim" class="my-input"
     aria-describedby="inputDescription" />
</form>

//sample Controller
<script>
  angular.module('inputExample', [])
   .controller('ExampleController', ['$scope', function($scope) {
    $scope.val = '1';
  }]);
</script>

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