AngularJS - 无法在字符串上创建属性方法 [英] AngularJS - cannot create property method on string
问题描述
我正在发送一个正常的获取请求,如下所示:
I am sending a normal get request as follows which was working normally:
service.show = function (slug) {
console.log(slug)
return $http.get('api/packages/'+slug, slug).success(function(response){
service.package = response.package;
});
};
在我开始使用 gulp 将所有 js 文件组合在一起后,突然出现了这个奇怪的错误:
Suddenly after I started combining all js files together using gulp I get this weird error:
angular.js:10147 TypeError: Cannot create property 'method' on string 'package-38'
由于该应用程序是一个 api,所以我尝试使用邮递员运行请求,以查看它是否正常工作.但是,当在某个页面中调用相同的请求时,我收到此错误.
Since the application is an api so I tried to run the request using postman to see if it's working and it was. However when the same request is called in a certain page, I get this error.
我检查变量 slug
是一个 string
并且它被正常发送,那么这里可能是什么错误?!
I check variable slug
which is a string
and it is sent normally, so what could be the possible bug here?!
推荐答案
$http.get
期望的第二个参数,是一个带有 http 请求配置的对象.
The second parameter $http.get
expects, is an object with http request configuration.
查看 angular 文档.请参阅快捷方式部分.
Have a look at angular documentation. See the Shortcut methods section.
$http.get('/someUrl', config).then(successCallback, errorCallback);
$http.get('/someUrl', config).then(successCallback, errorCallback);
或者更好且可读的调用如下所示:
Or better and readable call looks like below:
$http({
method: 'GET',
url: 'api/packages/'+slug
}).then(function(response){
service.package = response.package;
}, function errorCallback(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
这篇关于AngularJS - 无法在字符串上创建属性方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!