簡體   English   中英

如何將URL中的角度JS值作為參數傳遞

[英]How to pass angular JS value in URL as parameters

我有以下鏈接

<a href="entity-details.html?id='{{ x.community_id }}&community_society_id={{x.community_society_id}}">View</a>

我希望將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;
    });
});

您需要使用ng-href,以便可以將值內置到鏈接中。 Lke @tanmay在評論中說。

<a ng-href="entity-details.html?id={{ x.community_id }}&community_society_id={{x.community_society_id}}">View</a>

更改控制器以正確解析網址。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM