简体   繁体   English

如何在AngularJs中将某些数据从View传递到控制器

[英]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. 我想将组ID传递回控制器,以便我可以运行另一个使用此groupId来获取联系人的API。 I tried Using cookies for this but i receive an error 我尝试为此使用Cookie,但收到错误

`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: 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 作为参考,您可以检查-: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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