angularjs
  • arrays
  • 2017-04-13 119 views 0 likes 
    0

    我有下面的鏈接如何在URL傳遞角JS的值作爲參數

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

    ,而不是'href',使用'NG-href' – tanmay

    +0

    好,但我認爲,在錨標記語法是錯誤的,我認爲u能解決這個問題? –

    +0

    如果它是ng模型的值,那麼你不必將它傳遞給控制器​​,範圍變量將在你的控制器中可用 – CrazyMac

    回答

    0

    您需要使用NG-HREF這樣的值可以內置的鏈接。 Lke @tanmay在評論中表示。

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

    更改控制器以正確解析URL。

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

    但是在控制器中我正在接收一個像這樣的垃圾的值.. 3&community_society_我只想要3個而不是community_society_。我認爲我卡在錨標記中..你解決這個問題。? –

    +0

    你有一個額外的'在你張貼的href。 – Michael

    +0

    我刪除了它..但在控制器中獲取垃圾價值。我不知道爲什麼。? –

    相關問題