2015-09-25 80 views
0

得到更新我有這樣的控制器代碼:新添加的元素在HTML

$scope.searchResult = [{title:"this is 1"},{title:"this is 2"}]; 

$scope.searchByTag = function() 
{ 
    $location.path("search"); 

    var req = {     
     method: 'POST', 
     url: 'url', 
     headers: { 
     'Content-Type': "application/json" 
     }, 
     data: searchFormService.getTagSearhReqData() 
    }; 

    $http(req).then(function(d){   
     for(var i=0;i<d.data.length;i++) 
     { 
     $scope.searchResult.push(d.data[i]);    
     }   
     alert($scope.searchResult.length); 
    }, function(e){ 
    });  
} 

下面是HTML代碼:

<div ng-repeat="data in searchResult"> 
      {{data.title}} 
    </div> 

最初,它顯示title1 & title2searchByTag()被稱爲alert($scope.searchResult.length);展示新searchResult的長度意味着新元素被添加到searchResult,但它不會在html中更新。

+0

您的代碼應該工作,但也許它掛在警報(直到你按確定)?嘗試註釋該警報或將其轉換爲'console.log'並查看會發生什麼。或者'd.data [i]'不包含'title'屬性?你可以通過'console.log(d.data [i])'來檢查。 – DzinX

+0

內循環'console.log(d.data [i] .title);'效果很好&我試圖沒有警報也 – manish

+0

你可以分享jsfiddle嗎? –

回答

0

您的JSON結構就是這樣

$scope.searchResult.push({'title':d.data[i]});