我在 Angular 中创建了一个简单的日志记录服务:
app.service('$userInteraction', [function() {
var userLog = "";
this.log = function (datetime, screen, logMessage) {
userLog.concat(datetime + "\t" + screen + " Screen\t" + logMessage + "\n");
}
this.getLog = function () {
return userLog;
}
this.clearLog = function () {
userLog = "";
}
}]);
我在我的一个 Controller 中使用它,就像这样:
app.controller('myController', ['$scope', '$userInteraction', function($scope, $userInteraction) {
$userInteraction.log(Date(), 'Login', 'Some random message.');
}]);
但是当我运行我的代码时,出现以下错误:
TypeError: $userInteraction.log is not a function
虽然我可以发誓它以前有效。我是 Angular 的新手,所以这很可能是一个新手错误。提前致谢!
最佳答案
这个问题很老,但是可以帮助某人...您的代码在 plunker 上运行,可能您有重复的服务名称...检查所有文件 angularjs 并验证是否存在另一个具有相同名称的服务。
我遇到了类似的问题。具有相同名称和调用的两个服务没有调用函数。
关于javascript - Angular 服务不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307733/