【发布时间】:2017-09-24 13:56:31
【问题描述】:
我第一次尝试将 Coffeescript 与 AngularJS 一起使用。
我想定义一个依赖于服务$http的新服务
这是我所期待的:
var MyService = function($http) {
this.$http = $http;
};
MyService.prototype.call = function(url, data) {
this.$http(url, data);
};
myApp.service("webService", MyService)
这是注册服务的正常方式,如 AngularJS 文档中所示。
在阅读了一篇关于使用 Coffeescript 和 AngularJS 的文章后,我尝试了这个:
myApp.service "webService", class
constructor : (@$http) ->
call : (url, data) -> @$http url, data
但是编译的结果给出了这个javascript:
myApp.service("webService", (function() {
function _Class(_at_$http) {
this.$http = _at_$http;
}
_Class.prototype.call = function(url, data) {
return this.$http(url, data);
};
return _Class;
})());
问题是Coffeescript 编译器不应该用_at_$http 替换@$http。在我的情况下,它应该输出这个 javascript:
myApp.service("webService", (function() {
function _Class($http) {
this.$http = $http;
}
_Class.prototype.call = function(url, data) {
return this.$http(url, data);
};
return _Class;
})());
你可以看到 online compiler on Coffeescript website 给出了预期的结果,所以我不明白为什么我的工作不好。
我需要这个,因为角喷射引擎无法识别 _at_$http,因为它需要 $http 参数名称。
【问题讨论】:
标签: javascript angularjs coffeescript