如何在 angularJs 中将一些数据从视图传递到控制器

How to pass some data from View to controller in angularJs

本文关键字:视图 控制器 数据 angularJs      更新时间:2023-09-26
 `<tr ng-repeat="x in rest">
   <td>{{ x.groupId }}</td>
    <td><a  href="#/offers">{{ x.groupName }}</a></td>
  </tr>`

这是我的观点的代码。我想将组 ID 传回控制器,以便我可以运行另一个使用此 groupId 获取联系人的 API。我尝试为此使用 cookie,但收到错误

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

将数据传递到控制器非常简单。您可以按如下方式进行操作-:

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

路由配置:

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

控制器代码:

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

作为参考,您可以查看 -:https://docs.angularjs.org/tutorial/step_07

希望这有帮助。

您可以使用以下命令将数据从视图传递到控制器

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>