[英]How to pass angular JS value in URL as parameters
I have the following link 我有以下链接
<a href="entity-details.html?id='{{ x.community_id }}&community_society_id={{x.community_society_id}}">View</a>
I want x.community_id and x.community_society_id values to pass to a controller then later I fetch the data. 我希望将x.community_id和x.community_society_id值传递给控制器,然后再获取数据。
var app = angular.module('EntityApp', []);
app.controller('EntityAppCntroller', function($scope, $http, $location) {
var myParamtr = location.search.split('id=')[1] ? location.search.split('id=')[1] : 'myDefaultValue';
alert(myParamtr);
$http.get('apartment/community/details/list/'+ myParam).then(function(response) {
$scope.myData = response.data.list;
});
});
You need to use ng-href so the values can be built into the link. 您需要使用ng-href,以便可以将值内置到链接中。 Lke @tanmay said in the comments.
Lke @tanmay在评论中说。
<a ng-href="entity-details.html?id={{ x.community_id }}&community_society_id={{x.community_society_id}}">View</a>
change the controller to parse the url correctly. 更改控制器以正确解析网址。
var app = angular.module('EntityApp', []);
app.controller('EntityAppCntroller', function($scope, $http, $location) {
var getJsonFromUrl = function(url) {
console.log(url);
var query = url.substr(1);
var result = {};
query.split("&").forEach(function(part) {
var item = part.split("=");
result[item[0]] = decodeURIComponent(item[1]);
});
return result;
};
var queryParams = getJsonFromUrl(location.search);
var myParamtr = queryParams.id ? queryParams.id : 'myDefaultValue';
alert(myParamtr);
$http.get('apartment/community/details/list/'+ myParam).then(function(response) {
$scope.myData = response.data.list;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.