RT
目标是模拟异步请求利用服务,将hellos输出
var myApp = angular.module('myApp', []);
myApp.controller('MyCtrl',['helloWorld',MyCtrl]);
myApp.provider('helloWorld', function() {
var _name = 'Default';
this.$get = function($q) {
return {
sayHello: function() {
var deferred = $q.defer()
setTimeout(function() {
if (_name !== 'Default') {
deferred.resolve('Hello, ' + _name + '!');
} else {
deferred.reject('No changes');
}
}, 3000);
return deferred.promise;
}
}
};
this.setName = function(newName) {
_name = newName;
}
});
myApp.config(function(helloWorldProvider){
helloWorldProvider.setName('World');
});
function MyCtrl(helloWorld) {
helloWorld.sayHello().then(function(text){
this.hellos = text
},function(msg){
this.hellos = msg
})
}
此时将hellos输出
控制台没有报错,视图也不显示,请问是什么原因?
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
低级错误……
很久没写ng了,很多东西都忘了,不知道对不对。
vm通过
$scope