AngularJS - 不能在字符串上创建属性方法 [英] AngularJS - cannot create property method on string

查看:679
本文介绍了AngularJS - 不能在字符串上创建属性方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发送一个普通的get请求,如下所示正常工作:

  service.show = function(slug){ 
console.log(slug)
return $ http.get('api / packages /'+ slug,slug).success(function(response){
service.package = response.package ;
});
};

突然,我开始使用gulp将所有js文件组合在一起,结果出现这个奇怪的错误:

  angular.js:10147 TypeError:无法在字符串'package-38'上创建属性'method'

由于应用程序是api,因此我试图使用邮递员运行请求以查看它是否正常工作。但是,当在某个页面中调用相同的请求时,出现此错误。



我检查变量 slug which是一个字符串,它是正常发送的,所以这里可能有什么bug?!

解决方案第二个参数 $ http.get 期望是一个具有http请求配置的对象。



查看角度文档
$ b


$ http.get('/ someUrl',config).then(successCallback,errorCallback);

p>

或者更好且可读的调用如下所示:

  $ http({
method:'GET',
url:'api / packages /'+ slug
})。then(function(response){
service.package = response.package;
},函数errorCallback(response){
//如果发生错误,则异步调用
//或服务器返回错误状态的响应
});


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;
            });
        };

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'

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.

I check variable slug which is a string and it is sent normally, so what could be the possible bug here?!

解决方案

The second parameter $http.get expects, is an object with http request configuration.

Have a look at angular documentation. See the Shortcut methods section.

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆