简体   繁体   中英

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.

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. Lke @tanmay said in the comments.

<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;
    });

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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