我正面临来自 angularjs 的奇怪行为。
.factory('configService', function($http){
var base = 'http://Harold:Pituca521zkjOidksjdIIQUdjsdh120@localhost:3000/configuration/';
var getConfig = function(){
return $http.get(base + 'config');
};
var setConfig = function(config){
return $http.post(base + 'update', config);
};
return {
getConfig: getConfig,
setConfig: setConfig
};
})
.controller('ConfigurationController', function($scope, $http, $window, configService){
$scope.config = {};
configService.getConfig()
.success(function(data, status, headers, config){
console.log(data);
$scope.config = data;
});
$scope.saveConfiguration = function(config){
configService.getConfig(config)
.success(function(data, status, headers, config){
$window.location.reload();
});
};
});
当我执行 console.log(data) 时,我从我的本地主机获取一个 URL 而不是一个从未被命中的对象。
/Users/rodrigoqueirolo/Desktop/factory/public/index.html
我认为问题是因为带有凭据的 URL,但令人惊讶的是我还有其他 6 条路由在做同样的事情 (CRUD)。这是我单击输入表单时的完整错误。
TypeError: Cannot assign to read only property 'daily_cota_premium_user' of /Users/user/Desktop/factory/public/index.html
at Oa (file://localhost/Users/user/Desktop/final/admin/angular.min.js:102:253)
at Function.d.assign (file://localhost/Users/user/Desktop/final/admin/angular.min.js:104:22)
at O (file://localhost/Users/user/Desktop/final/admin/angular.min.js:210:465)
at $$writeModelToScope (file://localhost/Users/user/Desktop/final/admin/angular.min.js:215:271)
at file://localhost/Users/user/Desktop/final/admin/angular.min.js:215:209
at k (file://localhost/Users/user/Desktop/final/admin/angular.min.js:213:285)
at g (file://localhost/Users/user/Desktop/final/admin/angular.min.js:213:215)
at $$runValidators (file://localhost/Users/user/Desktop/final/admin/angular.min.js:213:499)
at $$parseAndValidate (file://localhost/Users/user/Desktop/fina/admin/angular.min.js:215:130)
at $commitViewValue (file://localhost/Users/user/Desktop/final/admin/angular.min.js:214:272)
谢谢
最佳答案
我不是 JavaScript 专家,但这是我的看法:
更改服务的返回以返回运行函数的函数(听起来很奇怪)
.factory('configService', function($http){
...
return {
getConfig: function(){
return getConfig();
},
setConfig: function(config){
return setConfig(config);
}
};
})
这应该会返回一个您可以使用的 promise 。
但也许我错了,看你的代码,它应该按照你的要求去做。
关于javascript - 奇怪的行为 angularjs 返回 "TypeError: Cannot assign to read only property",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26897667/