Angular $scope.value 在通过控制器评估和发送值时未更新
Angular $scope.value not updated when value is evaluated and sent via controller
这是Fiddle我用来学习angularjs
简而言之,JS
个正在使用的文件:
angular.module('ngApp', [])
.service('myownservice', '$q', function ($timeout, $q) {
this.httpcall = function() {
var httpresp = "1818";
//making an http call over here.
return httpresp;
};
this.UpdateSomeData = function () {
var defer = $q.defer();
myownservice.httpcall().then(function(data) {
defer.resolve(data);
});
return defer.promise;
};
})
.controller('ctrl', function ($scope, myownservice) {
$scope.value = UpdateSomeData();
});
html page
:
<div ng-controller="ctrl">{{value}}</div>
但我收到类似
的错误
Argument 'fn' is not a function, got string
.
知道为什么吗?
你不应该只是 return 那样的承诺。尝试这样的事情:
this.returnUpdateSomeData = function () {
this.UpdateSomeData().then(function(data) {
return data;
});
};
你错过了控制器中的 myownservice
$scope.value = myownservice.returnUpdateSomeData ();
我认为这是调用 HTTP 服务和 return 其响应的一种非常通用的方法。请试试这个
angular.module('ngApp', []).service('myownservice', '$q', function ($timeout, $q) {
this.UpdateSomeData = function () {
return $http.get('URL');
};
})
app.controller('ctrl', function ($scope, myownservice) {
$scope.value = myownservice.UpdateSomeData(); // Depends on the
response, your $scope.value object has to be declared
});
这有多个问题。
首先,您在 myownservice
中的注射没有正确提供 [
和 ]
以及 $timeout
。
接下来,在服务中,您需要通过 this
访问自身而不是命名自身。
接下来,您需要 return 来自 httpcall
方法的承诺,而不仅仅是数字。
它应该是这样的,
angular.module('ngApp', [])
.service('myownservice', ['$q', function($q) {
this.httpcall = function() {
var defer = $q.defer();
var httpresp = "1818";
defer.resolve(httpresp);
return defer.promise;
// replace all this with your $http call and return it..
// it returns promise itself so you wouldn't need to create on your own
};
this.UpdateSomeData = function() {
return this.httpcall();
};
}])
.controller('ctrl', function($scope, myownservice) {
myownservice.UpdateSomeData().then(function(val) {
$scope.value = val
})
});
这是Fiddle我用来学习angularjs
简而言之,JS
个正在使用的文件:
angular.module('ngApp', [])
.service('myownservice', '$q', function ($timeout, $q) {
this.httpcall = function() {
var httpresp = "1818";
//making an http call over here.
return httpresp;
};
this.UpdateSomeData = function () {
var defer = $q.defer();
myownservice.httpcall().then(function(data) {
defer.resolve(data);
});
return defer.promise;
};
})
.controller('ctrl', function ($scope, myownservice) {
$scope.value = UpdateSomeData();
});
html page
:
<div ng-controller="ctrl">{{value}}</div>
但我收到类似
的错误Argument 'fn' is not a function, got string
.
知道为什么吗?
你不应该只是 return 那样的承诺。尝试这样的事情:
this.returnUpdateSomeData = function () {
this.UpdateSomeData().then(function(data) {
return data;
});
};
你错过了控制器中的 myownservice
$scope.value = myownservice.returnUpdateSomeData ();
我认为这是调用 HTTP 服务和 return 其响应的一种非常通用的方法。请试试这个
angular.module('ngApp', []).service('myownservice', '$q', function ($timeout, $q) {
this.UpdateSomeData = function () {
return $http.get('URL');
};
})
app.controller('ctrl', function ($scope, myownservice) {
$scope.value = myownservice.UpdateSomeData(); // Depends on the
response, your $scope.value object has to be declared
});
这有多个问题。
首先,您在 myownservice
中的注射没有正确提供 [
和 ]
以及 $timeout
。
接下来,在服务中,您需要通过 this
访问自身而不是命名自身。
接下来,您需要 return 来自 httpcall
方法的承诺,而不仅仅是数字。
它应该是这样的,
angular.module('ngApp', [])
.service('myownservice', ['$q', function($q) {
this.httpcall = function() {
var defer = $q.defer();
var httpresp = "1818";
defer.resolve(httpresp);
return defer.promise;
// replace all this with your $http call and return it..
// it returns promise itself so you wouldn't need to create on your own
};
this.UpdateSomeData = function() {
return this.httpcall();
};
}])
.controller('ctrl', function($scope, myownservice) {
myownservice.UpdateSomeData().then(function(val) {
$scope.value = val
})
});