目的的希望出现loading,三秒后出现show,同时loading消失。但是貌似改变ngif值得时候没反应?
<body ng-app="demo">
<div ng-controller="crtl">
<div ng-if="!$kUser.isload.status">loading</div>
<div ng-if="$kUser.isload.status">show</div>
</div>
<script src='../bower_components/angular/angular-1.3.0.js'></script>
<script>
angular.module('demo', [])
.controller('crtl', ['$scope', '$kUser', function($scope, $kUser) {
$kUser.getList();
}])
.factory('$kUser', ['$rootScope', function($rootScope) {
var _this = {};
_this.isload = {
status:false
};
_this.getList = function() {
console.log(11)
setTimeout(function() {
$rootScope.$apply(function() {
console.log(_this.isload.status)
_this.isload = {
status:true
};
console.log(333)
console.log(_this.isload.status)
})
}, 3000);
}
return _this;
}])
</script>
首先,你html直接用了这个服务$kUser,那么你controller中要:
其次不明白你此处为什么要用服务,我的建议是尽量不要让view直接去双向绑定服务。如下两种解决方式感觉更好:
1.不要在factory中去做,直接在controller中做定时回调。
2.如果非要用服务,那么只能controller依赖服务,不要再服务中操作view。你可以将服务写成promise,在controller中用。