AngularJS数据仅在重新加载后显示

时间:2016-12-01 17:22:07

标签: angularjs ionic-framework

我的数据来自JSON(链接有待观察)

我可以在我的日志中看到数据已加载

我无法在我的模板中显示值(需要重新加载要显示的浏览器)

从底部可以看出,我的模板在标题中非常简单selectedCategories.nameevents

首先在重新加载页面后显示,第二个从不显示。

感谢您的帮助

工厂:

.factory('selectedCategoriesService', function($http, $stateParams) {
        var selectedCategories = [];

        return {
        getselectedCategories: function(){
            return $http.get('http://www.myapi.com&data=categorie', { params: { cat: $stateParams.id } }).then(function(response){
                selectedCategories = response.data;
                delete selectedCategories.api;
                return response.data;
            });
        },
        getselectedCategorie: function(selectedCategorieId){
            for(i=0;i<selectedCategories.length;i++){
                if(selectedCategories[i].id == selectedCategorieId){
                    return selectedCategories[i];
                }
            }
        }
    }
})

控制器:

.controller('CategoriesListDetailCtrl',function($scope, $stateParams, selectedCategoriesService, $ionicModal){
  selectedCategoriesService.getselectedCategories().then(function(selectedCategories){
    $scope.selectedCategories = selectedCategories[0];
    console.log("Categorie = " + JSON.stringify(selectedCategories[0]));
    })
})

模板:

<ion-pane>

  <ion-view view-title="{{selectedCategories.name}}">

    <ion-content class="has-header">

        <ion-item class="item-thumbnail-left" collection-repeat="event in events | DateDivider" divider-collection-repeat>

           <h2>{{event.title}}</h2>

        </ion-item>

    </ion-content>

  </ion-view>

</ion-pane>

2 个答案:

答案 0 :(得分:0)

这可能是一个摘要周期问题,您可能需要使用$scope.$apply()

.controller('CategoriesListDetailCtrl',function($scope, $stateParams, selectedCategoriesService, $ionicModal){
  selectedCategoriesService.getselectedCategories().then(function(selectedCategories){
    $scope.selectedCategories = selectedCategories[0];
    $scope.$apply()
    console.log("Categorie = " + JSON.stringify(selectedCategories[0]));
    })
})

答案 1 :(得分:0)

如果标题没有显示,那是因为在我的模板中我使用:

<ion-view>
    <ion-nav-title>{{content.title}}</ion-nav-title>
    ...

尽管:

update t
    set active = 0
    where created <= (select min(t2.created)
                      from t t2
                      where operation = @Operation
                     );

感谢@Dunc