我可以像这样获取数组,我可以在控制台上看到它
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
和每个数组都有这样的价值,例如0th Array包含此
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重复不起作用
控制台屏幕截图
完成了.. idk为什么,但是解决方案是如此糟糕
$scope.results = response.data;
$rootScope.vars = $scope.results
这可能很愚蠢,您能像这样写时写下API的结果,
$scope.keyup = function (data) {
$http({
url: 'myapi?q='+data,
method: "GET",
}).success(function(response){
$scope.results = response.data.data;
});
}
注意:您缺少关键功能上的闭合式。(有一个 将代码复制到堆栈时会错过的机会。如果是这样,请忽略。(
以下一个人可以帮助您。
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>
您的ng-repeat
代码按照提供的JSON
。
演示
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>
使用
的曲目<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>