2016-09-10 66 views
2
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' 
      } 
      } 
     }) 

在這裏我得到的數據$scope.post,但它不是在模板中反映。如果我使用$scope.$apply(),那麼我會收到錯誤$scope.$apply is not a function。我不知道爲什麼突然間我開始遇到這樣的問題。早些時候它工作正常。

回答

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

+1

正確!設置'$ scope = {...}' - 另一個對象沒有任何意義..這就像一個犯罪.. PO應該知道這一點。 – Paritosh

+0

只是將'$ scope = {}'改爲'$ scope.fields'解決了它。不需要執行'$ scope。$ apply' – manish