2016-09-10 27 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