$scope 在 $apply() 之后不刷新模板内的数据
$scope is not refreshing data inside template after $apply()
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
。我不知道为什么突然间我开始遇到这种问题。早些时候它工作正常。
尝试使用 $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
。
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
。我不知道为什么突然间我开始遇到这种问题。早些时候它工作正常。
尝试使用 $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
。