当我使用它时,ng-repeat不起作用

时间:2015-10-05 19:16:51

标签: angularjs onsen-ui

我的代码如下:

和script.js如下:

<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>

但ng-repeat不起作用。当我添加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); }, }); }); 时,即使在中间只显示&#34; 123&#34; ,它不会显示。

有人知道原因吗?

2 个答案:

答案 0 :(得分:1)

首先,你应该使用angular的$http服务而不是jQuery的$.ajax方法。来自docs

  

$ http服务是一个核心Angular服务,它通过浏览器的XMLHttpRequest对象或通过JSONP促进与远程HTTP服务器的通信。

所以你的代码应该更像这样:

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'
        }
    ]
}

答案 1 :(得分:-1)

而不是:

$scope.stocksSearchList = new Object();

尝试

$scope.stocksSearchList = [];

确保这会返回一个列表;