ng-repeat 在我使用时不起作用
ng-repeat does not work when I use it
我的代码如下:
<ons-template id="stockSearch.html">
<ons-page ng-controller="stockSymbolController">
<ons-toolbar class="DCF">
<div class="left">
<ons-back-button style="color:white;"></ons-back-button>
</div>
<div class="center" style="font-size:22px;" >Stock Search : {{myDCFNavigator.getCurrentPage().options.symbol}}</div>
</ons-toolbar>
<div class="stockSearchList">
<div ng-repeat="stockSearch in stocksSearchList">
<div class="stockSearchOne">
<div class="stockSearchOneComp">{{stockSearch.company}}</div>
<div class="stockSearchOneInfo">Symbol: {{stockSearch.symbol}}| Exchange:{{stockSearch.exchange}} </div>
</div>
</div>
</div>
</ons-page>
和script.js如下:
module.controller('stockSymbolController', function($scope, $http){
$scope.stocksSearchList = new Object();
var url = "https://www.xxxxx.com/api/public/search_company/"+myDCFNavigator.getCurrentPage().options.symbol;
$.ajax({
type: 'get',
url: url,
success: function(data) {
$scope.stocksSearchList = data;
console.log($scope.stocksSearchList);
},
});
});
但是 ng-repeat 没有 work.when 我添加了 <div ng-repeat="stockSearch in stocksSearchList">
即使在中间只显示“123”,它也不会显示。
有人知道原因吗?
而不是:
$scope.stocksSearchList = new Object();
尝试
$scope.stocksSearchList = [];
并确保这个 returns 是一个列表;
首先你应该使用angular的$http
服务而不是jQuery的$.ajax
方法。来自 docs:
The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.
因此您的代码应该更像这样:
module.controller('stockSymbolController', function($scope, $http){
var url = "https://www.xxxxx.com/api/public/search_company/"+myDCFNavigator.getCurrentPage().options.symbol;
$http.get(url)
.then(function(response) { // success is being deprecated in favour of .then
$scope.stocksSearchList = response.stocksSearchList; // this should be an array that has nested objects that include properties and values for company, symbol and exchange (see note below)
console.log($scope.stocksSearchList);
}, function(error) {
// do something with the error
});
});
我假设 response
看起来像下面的数组中有两个项目:
{
stocksSearchList: [
{
company: 'some company',
symbol: 'some symbol',
exchange: 'some exchange'
},
{
company: 'some other company',
symbol: 'some other symbol',
exchange: 'some other exchange'
}
]
}
我的代码如下:
<ons-template id="stockSearch.html">
<ons-page ng-controller="stockSymbolController">
<ons-toolbar class="DCF">
<div class="left">
<ons-back-button style="color:white;"></ons-back-button>
</div>
<div class="center" style="font-size:22px;" >Stock Search : {{myDCFNavigator.getCurrentPage().options.symbol}}</div>
</ons-toolbar>
<div class="stockSearchList">
<div ng-repeat="stockSearch in stocksSearchList">
<div class="stockSearchOne">
<div class="stockSearchOneComp">{{stockSearch.company}}</div>
<div class="stockSearchOneInfo">Symbol: {{stockSearch.symbol}}| Exchange:{{stockSearch.exchange}} </div>
</div>
</div>
</div>
</ons-page>
和script.js如下:
module.controller('stockSymbolController', function($scope, $http){
$scope.stocksSearchList = new Object();
var url = "https://www.xxxxx.com/api/public/search_company/"+myDCFNavigator.getCurrentPage().options.symbol;
$.ajax({
type: 'get',
url: url,
success: function(data) {
$scope.stocksSearchList = data;
console.log($scope.stocksSearchList);
},
});
});
但是 ng-repeat 没有 work.when 我添加了 <div ng-repeat="stockSearch in stocksSearchList">
即使在中间只显示“123”,它也不会显示。
有人知道原因吗?
而不是:
$scope.stocksSearchList = new Object();
尝试
$scope.stocksSearchList = [];
并确保这个 returns 是一个列表;
首先你应该使用angular的$http
服务而不是jQuery的$.ajax
方法。来自 docs:
The $http service is a core Angular service that facilitates communication with the remote HTTP servers via the browser's XMLHttpRequest object or via JSONP.
因此您的代码应该更像这样:
module.controller('stockSymbolController', function($scope, $http){
var url = "https://www.xxxxx.com/api/public/search_company/"+myDCFNavigator.getCurrentPage().options.symbol;
$http.get(url)
.then(function(response) { // success is being deprecated in favour of .then
$scope.stocksSearchList = response.stocksSearchList; // this should be an array that has nested objects that include properties and values for company, symbol and exchange (see note below)
console.log($scope.stocksSearchList);
}, function(error) {
// do something with the error
});
});
我假设 response
看起来像下面的数组中有两个项目:
{
stocksSearchList: [
{
company: 'some company',
symbol: 'some symbol',
exchange: 'some exchange'
},
{
company: 'some other company',
symbol: 'some other symbol',
exchange: 'some other exchange'
}
]
}