在$ apply()之后$ scope不刷新模板内的数据

时间:2016-09-10 06:46:47

标签: javascript angularjs ionic-framework object-literal

override func prepare() {
    if self.collectionView?.numberOfSections == 0 {
        return
    }

    if (self.itemAttributes.count > 0) {
        for section in 0..<self.collectionView!.numberOfSections {
            let numberOfItems : Int = self.collectionView!.numberOfItems(inSection: section)
            for index in 0..<numberOfItems {
                if section != 0 && index != 0 {
                    continue
                }

                let attributes : UICollectionViewLayoutAttributes = self.layoutAttributesForItem(at: IndexPath(item: index, section: section))!
                if section == 0 {
                    var frame = attributes.frame
                    frame.origin.y = self.collectionView!.contentOffset.y
                    attributes.frame = frame
                }

                if index == 0 {
                    var frame = attributes.frame
                    frame.origin.x = self.collectionView!.contentOffset.x
                    attributes.frame = frame
                }
            }
        }
        return
    }

    if (self.itemsSize == nil || self.itemsSize.count != numberOfColumns) {
        self.calculateItemsSize()
    }

    var column = 0
    var xOffset : CGFloat = 0
    var yOffset : CGFloat = 0
    var contentWidth : CGFloat = 0
    var contentHeight : CGFloat = 0

    for section in 0..<self.collectionView!.numberOfSections {
        var sectionAttributes: [UICollectionViewLayoutAttributes] = []

        for index in 0..<numberOfColumns {
            let itemSize = (self.itemsSize[index] as AnyObject).cgSizeValue
            let indexPath = IndexPath(item: index, section: section)
            let attributes = UICollectionViewLayoutAttributes(forCellWith: indexPath)
            attributes.frame = CGRect(x: xOffset, y: yOffset, width: (itemSize?.width)!, height: (itemSize?.height)!).integral

            if section == 0 && index == 0 {
                attributes.zIndex = 1024;
            } else  if section == 0 || index == 0 {
                attributes.zIndex = 1023
            }

            if section == 0 {
                var frame = attributes.frame
                frame.origin.y = self.collectionView!.contentOffset.y
                attributes.frame = frame
            }
            if index == 0 {
                var frame = attributes.frame
                frame.origin.x = self.collectionView!.contentOffset.x
                attributes.frame = frame
            }

            sectionAttributes.append(attributes)

            xOffset += (itemSize?.width)!
            column += 1

            if column == numberOfColumns {
                if xOffset > contentWidth {
                    contentWidth = xOffset
                }

                column = 0
                xOffset = 0
                yOffset += (itemSize?.height)!
            }
        }
        self.itemAttributes.append(sectionAttributes)
    }

    if let attributes = self.itemAttributes.last?.last {
        contentHeight = attributes.frame.origin.y + attributes.frame.size.height
        self.contentSize = CGSize(width: contentWidth, height: contentHeight)
    }
}

这是我的路由器

var itemAttributes : [[UICollectionViewLayoutAttributes]]!

我在这里获取614 fix #43 Jack 2 hours age 613 Complete feature XX Tom 1 min ago 的数据,但它没有在模板中反映出来。如果我使用angular.module("mobApp.controllers") .controller("ViewPostController",function($scope , $stateParams, Utils, PublishMessageService, $state, $ionicActionSheet, $ionicModal, Constants, ShareObjectService) { var postId = $stateParams.postId; $scope.post = {}; $scope.BASE_URL = Constants.SERVER_URL; $scope.$on('$ionicView.loaded', function() { $scope.utils = Utils; resetScopeVariables(); loadPost(); }); $scope.reload = function() { loadPost(); } $scope.vote = function(eventSource, voteType) { Utils.vote(eventSource, voteType, postId, postId); } loadPost = function() { resetScopeVariables(); if(Utils.hasInternet()) { PublishMessageService.viewPublishMessage(postId).then(function(d){ if(d.data.post) { $scope.showPost = true; $scope.post = d.data.post; $scope.showContentLoading = false; ShareObjectService.setPostPhotoIds($scope.post.photosIds); } else { showInlineMessage(Utils.prepareErrorMessage("Nothing Was Found", "Sorry requested content is not available.")); } }, function(err) { showInlineMessage(Utils.prepareErrorMessage("Sorry!", err.description)); }); } else { showInlineMessage(Utils.prepareErrorMessage("Can't Connect", Constants.MSG_NO_INTERNET)); $scope.showReloadBtn = true; } } $scope.showPostMoreOptions = function(postId) { $ionicActionSheet.show({ buttons: [ { text: '<i class="icon ion-edit"></i> Edit' }, { text: '<i class="icon ion-trash-a"></i> Delete' } ], buttonClicked: function(index) { if(index === 0) { $state.go('app.publish-message-form', {'edit': true, 'postId': postId}); } else if(index === 1) { } return true; }, destructiveButtonClicked: function() { return true; } }); } /* Utils function */ function resetScopeVariables() { $scope = { pageInlineMsg: '', contentLoadingMessage: 'Loading..', showReloadBtn: false, showContentLoading: true, showPost: false }; } function showInlineMessage(msg) { $scope.pageInlineMsg = msg; $scope.showContentLoading = false; } }); ,则会收到错误 $stateProvider .state('app', { url : '/app', abstract : true, templateUrl: 'templates/globalLeftMenu.html', controller: 'GlobalLeftMenuController' }) .state('app.view-post', { url: '/view-post/:postId', views: { 'app': { templateUrl: 'templates/publish_message/view_post.html', controller: 'ViewPostController' } } }) 。我不知道为什么突然间我开始遇到这种问题。早些时候它工作正常。

1 个答案:

答案 0 :(得分:3)

使用$scope.$apply()尝试并更改

function resetScopeVariables() {
    $scope = {
        pageInlineMsg: '',
        contentLoadingMessage: 'Loading..',
        showReloadBtn: false,
        showContentLoading: true,
        showPost: false
    };    
}

function resetScopeVariables() {
    $scope.pageInlineMsg = '';
    $scope.contentLoadingMessage = 'Loading..';
    $scope.showReloadBtn = false;
    $scope.showContentLoading = true;
    $scope.showPost = false;  
}

如前所述,您将$scope设置为一个全新的对象,因此无法再调用$scope.$apply