![](/img/trans.png)
[英]YII: How to pass list of parameters from js to controller and call new view by passing another list?
[英]Passing parameters from view to another view with another controller
我试图调用REST API,将两个参数传递给另一个视图和另一个控制器。
语法为/api/:id/something/:id2/something
。
在接收端,我有一个工厂将处理该调用并将其传递给该新页面的控制器。
这是我的看法:
<div>
<table class="table table-striped">
<thead>
<th style="padding-left: 20px;">ACN Name</th>
<th>Product Name</th>
<th>ContractNumber</th>
<th>Start Date</th>
<th>End Date</th>
<th></th>
<tr ng-repeat="row in acns">
<td style="padding-left: 20px;"><a href="" ng-click="viewOrgList(ACNID)">{{row.ACNName}}</a></td>
<td>{{row.ProductName}}</td>
<td>{{row.ContractNumber}}</td>
<td>{{row.ContractStartDate}}</td>
<td>{{row.ContractEndDate}}</td>
<td><a href="" ng-click="remove(row)"><i class="fa fa-times" style="font-size:1.5em;"></i></a></td>
</tr>
</table>
</div>
该视图的控制器如下所示:
app.controller('acnController', ['$scope','$location', '$routeParams',
function ($scope, $location, $routeParams) {
//Passing acnId to networkOrg template to display.
$scope.viewOrgList = function (acnId) {
$location.path('/NetworkOrgs' + acnId);
}
}]);
在/ NetworkOrgs模板的控制器上,它看起来像这样:
app.controller('orgController', ['$scope', '$routeParams','$location', 'orgFactory', function ($scope, $routeParams, $locatoin, orgFactory) {
//Capture routeParam id and display associated orgs
$scope.org = $routeParams.acnId;
}]);
app.factory('orgFactory', ['$resource','$routeParams', function ($resource, $routeParams) {
return $resource("http://localhost:16047/api/acn/:acnId/networkOrgs", { acnId: $routeParams.acnId }, {
getOrgs: { method: 'GET', params: { acnId: '@acnId'} }
})
}])
这对我不起作用,并且我得到“错误参数”结果。
任何帮助将是巨大的!
我敢打赌,您在视图中错误地传递了参数。
你有 :
ng-click="viewOrgList(ACNID)"
你应该有 :
ng-click="viewOrgList(row.ACNID)"
您应该使用ng-click="viewOrgList(row.ACNID)"
而不是ng-click="viewOrgList(ACNID)"
并且应该
$scope.viewOrgList = function (acnId) {
$location.path('/NetworkOrgs/:' + acnId);
}
代替
$scope.viewOrgList = function (acnId) {
$location.path('/NetworkOrgs' + acnId);
}
用这种方式尝试
途中:
$routeProvider.when('/NetworkOrgs/:acnId', {
templateUrl: 'Pages/acn/partials/network_orgs.html',
controller: 'orgController'
});
在控制器中:
app.controller('orgController', ['$scope', '$routeParams','$location', 'orgFactory', function ($scope, $routeParams, $locatoin, orgFactory) {
//Capture routeParam id and display associated orgs
$scope.acnId= $routeParams.acnId; // ensure that you have acnId
orgFactory.getOrgs($scope.acnId)
.$promise.then(function(response) {
console.log(response);// check response by console
});
}]);
在orgFactory中:
app.factory('orgFactory', ['$resource', function ($resource) {
return {
getOrgs: function(acnId) {
var getOrgsById = $resource('http://localhost:16047/api/acn/:acnId/networkOrgs', {acnId: '@acnId'}, {
'get': {method: 'GET'} // if you expect return array from server then use 'get': {method: 'GET', isArray: true}
});
return getOrgsById .get({acnId: acnId});
};
}]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.