2017-04-17 113 views
0

我可以這樣獲取數組,我可以看到它在我的控制檯NG重複不工作

0 : Object 
1 
: 
Object 
2 
: 
Object 
3 
: 
Object 
4 
: 
Object 
5 
: 
Object 
6 
: 
Object 
7 
: 
Object 
8 
: 
Object 
9 
: 
Object 
10 
: 
Object 
11 
: 
Object 
12 
: 
Object 
13 
: 
Object 
14 
: 
Object 
15 
: 
Object 
16 
: 
Object 
17 
: 
Object 
18 
: 
Object 

在每個陣列有值這樣的例如第0陣列包含此

0:object 
    id : 8726 
    name : "aman" 
    slug : "aman" 
    type : "Drug" 

角JS

$scope.keyup = function (data) { 
    $http({ 
     url: 'myapi?q='+data, 
    method: "GET", 
    }).success(function(response){ 

     $scope.results = response.data; 
    }); 


$scope.$watch('userInput',function(){ 
    var key =$scope.$$watchers[0].last; 
    $scope.keyup(key) }); //this is because ng-keypress was not working but this is not an issue 

HTML

<a ng-repeat="item in results" ng-href="#"> 
       <div>{{item.name}}</div> 
      </a> 

我可以看到在控制檯中的數組,但我不知道爲什麼NG重複工作不

Console screenshot


對其做.. IDK的爲什麼,但解決的辦法是這麼這麼這麼差

$scope.results = response.data; 
$rootScope.vars = $scope.results 
+0

是在控制器中的HTML標籤,其中KEYUP函數寫的? –

+0

'我可以像這樣取數組,我可以在我的控制檯上看到它'你的意思是'$ scope.results'嗎? –

+0

小例子演示這個問題將是很好的:http://plnkr.co/ –

回答

-1

利用軌跡由

<a ng-repeat="item in $scope.results track by $index" ng-href="#"> 
      <div>{{item.name}}</div> 
     </a> 

<a ng-repeat="item in $scope.results track by item.id" ng-href="#"> 
      <div>{{item.name}}</div> 
     </a> 
+0

不工作...... – doe

+0

它的工作... https://jsfiddle.net/mubo2wuj/ 你應該刪除$範圍 –

0

這可能是愚蠢的,你可以這樣寫,同時從如下API得到的結果,

$scope.keyup = function (data) { 
    $http({ 
     url: 'myapi?q='+data, 
     method: "GET", 
    }).success(function(response){ 
     $scope.results = response.data.data; 
    }); 
} 

注:你缺少你的KEYUP功能密切梅開二度。 (有 機會錯過,而複製代碼堆棧。請忽略,如果是這樣)

+0

是的,它是愚蠢的,它不工作 – doe

0

下面的一個可以幫助你。

var app = angular.module('exApp', []); 
 
    app.controller('Ctrl', ['$scope', '$http', function($scope, $http){ 
 
    $scope.searchMov = "Batman"; 
 

 
     $scope.data = [{'name':'aa','id':1, 'address':'xxx'},{'name':'bb','id':2, 'address':'zzz'},{'name':'cc','id':3, 'address':'aaaa'},{'name':'dd','id':4, 'address':'bbbb'}]; 
 
      
 
    // console.log($scope.data); // see console 
 

 
$scope.keyup = function() { 
 
    $http({url: 'http://www.omdbapi.com/?s='+$scope.searchMov+'&page=1', 
 
     method: "GET", 
 
    }).success(function(response){ 
 
     $scope.results = response; 
 
    }); 
 
}; 
 
    function init(){ 
 
    $scope.keyup(); 
 
    }; 
 
    init(); 
 
    }]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body ng-app="exApp" ng-controller="Ctrl"> 
 
    <div> 
 
    {{data}}<br><br> 
 
    <input type="text" ng-model="search"> 
 
    <div ng-repeat="dat in data | filter: search">{{dat.id}} {{dat.name}} {{dat.address}}</div> 
 
    </div> 
 
    <br><br> 
 
    Search Movies Here: <input type="text" ng-model="searchMov" ng-change="keyup()"> 
 
    
 
    <p>{{results.Error}}</p> 
 
    <div ng-repeat="rs in results.Search"> 
 
    <div style="padding:2px;margin:2px;border:1px solid #789898"><p>{{rs.Title}} {{rs.Year}}</p> 
 
    <img ng-src="{{rs.Poster}}" style="width:100px;height:100px"> 
 
    </div></div> 
 
</body>

0

ng-repeat代碼工作正常,按提供的JSON

DEMO

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('MyCtrl', function($scope) { 
 
    var response = [ 
 
    {  
 
     id : 8726, 
 
     name : "aman", 
 
     slug : "aman", 
 
     type : "Drug" 
 
    }, 
 
    { 
 
     id : 8727, 
 
     name : "aman1", 
 
     slug : "aman1", 
 
     type : "Drug1"  
 
    }, 
 
    { 
 
     id : 8728, 
 
     name : "aman2", 
 
     slug : "aman2", 
 
     type : "Drug2"  
 
    }, 
 
    { 
 
     id : 8729, 
 
     name : "aman3", 
 
     slug : "aman3", 
 
     type : "Drug3"  
 
    } 
 
    ]; 
 
    
 
    $scope.results = response; 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp" ng-controller="MyCtrl"> 
 
    <a ng-repeat="item in results" ng-href="#"> 
 
     <div>{{item.name}}</div> 
 
    </a> 
 
</div>