[英]How to pass data from controller javascript to view javascript in AngularJS?
[英]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>`
这是我的观点的代码。 我想将组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>
RouteConfig:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.