Javascript 为什么我的Angularjs服务不允许我调用它?
我想做一个角度服务,它包含了常见的功能。 我将代码捆绑在我的MVC应用程序中:Javascript 为什么我的Angularjs服务不允许我调用它?,javascript,angularjs,Javascript,Angularjs,我想做一个角度服务,它包含了常见的功能。 我将代码捆绑在我的MVC应用程序中: bundles.Add(new ScriptBundle("~/bundles/Angular") .IncludeDirectory("~/app", "*.js", true)); 如果开发者工具真的把Common.js带进了我的公共文件夹,我就签入了开发者工具: 我在应用程序中添加了公用项: var app = angular.module('app', [
bundles.Add(new ScriptBundle("~/bundles/Angular")
.IncludeDirectory("~/app", "*.js", true));
如果开发者工具真的把Common.js带进了我的公共文件夹,我就签入了开发者工具:
我在应用程序中添加了公用项:
var app = angular.module('app',
[
'JobCtrl',
'JobSvc',
'WebsiteCtrl',
'WebsiteSvc',
'myClientCtrl',
'ClientSvc',
'MediaCompanyCtrl',
'MediaCompanySvc',
'PageAlertSvc',
'ui.bootstrap',
'ui.bootstrap.tpls',
'Common'
]
))
以及控制器:
angular.module('app', ['ui.bootstrap', 'ui.bootstrap.tpls'])
.controller('JobCtrl',
[
'JobService',
'WebsiteService',
'MediaCompanyService',
'ProductService',
'$scope',
'$uibModal',
'PageAlertService',
'Common',
function (JobService, WebsiteService, MediaCompanyService,
ProductService, $scope, $uibModal,PageAlertService, Common)
这就是我的Common.js文件的样子:
angular.module('app')
.service('Common', function () {
this.heyThere = function ()
{
console.log('Just wanted to say hey there')
};
});
无论何时在我的JobCtrl中调用它,我都会得到一个错误:$injector:unpr
未知提供程序
有人能看到我在无法识别我的Common.js文件的地方做错了什么吗?当我将Common.js移动到Services文件夹并尝试在我的控制器中调用它时,它会工作,但当它位于我的Common文件夹中时不会工作。没道理
提前谢谢 这仅仅是因为你正在定义你的应用程序..两次
angular.module('app', []) // this is where you re-define your app
.service('Common', function () {
this.heyThere = function ()
{
console.log('Just wanted to say hey there')
};
});
应该是:
angular.module('app')
.service('Common', function () {
this.heyThere = function ()
{
console.log('Just wanted to say hey there')
};
});
模块功能有两种模式。。使用2个参数设置应用程序。。只需一个参数,您就可以获得对现有应用程序的引用(之前已经定义过)这仅仅是因为您正在定义应用程序..两次
angular.module('app', []) // this is where you re-define your app
.service('Common', function () {
this.heyThere = function ()
{
console.log('Just wanted to say hey there')
};
});
应该是:
angular.module('app')
.service('Common', function () {
this.heyThere = function ()
{
console.log('Just wanted to say hey there')
};
});
模块功能有两种模式。。使用2个参数设置应用程序。。只需一个参数,您就可以获得对现有应用程序(之前已定义)的引用。使用模块的声明时请小心。您基本上是将
app
模块重新分配给不同的实例
angular.module('app', [dependencies]) //Constructs a module with dependencies
angular.module('app').service(...) //Associates the components (service)
//with the app module.
使用模块的声明时请小心。您基本上是将
app
模块重新分配给不同的实例
angular.module('app', [dependencies]) //Constructs a module with dependencies
angular.module('app').service(...) //Associates the components (service)
//with the app module.
虽然你的答案应该是正确的,但我改变了我的服务,以反映你所说的(遗憾的是,我应该更加小心如何称呼我的angular应用程序),它仍然给我同样的错误!首先,你可以随时投票给任何你喜欢的答案:)第二,你确定这是你唯一使用这种技术的地方吗?你是说像其他js技术一样的“技术”吗?是的,我现在只在app.js和我的控制器中调用它。一旦我把它从我的控制器中拔出,控制器就不再被识别。虽然你的答案应该是正确的,但我改变了我的服务,以反映你所说的(遗憾的是,我应该更加小心如何称呼我的angular应用程序),它仍然给我同样的错误!首先,你可以随时投票给任何你喜欢的答案:)第二,你确定这是你唯一使用这种技术的地方吗?你是说像其他js技术一样的“技术”吗?是的,我现在只在app.js和我的控制器中调用它。一旦我将其从控制器中拔出,控制器就不再被识别。谢谢您的回答,但即使进行了此更改,我仍然会收到相同的错误@Muli YulzaryCheck是否有具有相同
angular.module('app',[dependencies])
模式的其他文件。你的控制器文件和应用程序文件。我调用我的应用程序模块的唯一其他依赖位置是在我的控制器中。这些可能导致我的问题吗?正如我已经提到的,您需要在一个文件中指定依赖项一次。好的。我可能不得不问另一个问题,因为我现在不知道如何解决这个问题。我会接受你的答案,并可能开始一个新的。谢谢大家!@Muli YulzaryThank你的答案,但我仍然得到相同的错误,即使这个改变@Muli YulzaryCheck是否有具有相同angular.module('app',[dependencies])
模式的其他文件。你的控制器文件和应用程序文件。我调用我的应用程序模块的唯一其他依赖位置是在我的控制器中。这些可能导致我的问题吗?正如我已经提到的,您需要在一个文件中指定依赖项一次。好的。我可能不得不问另一个问题,因为我现在不知道如何解决这个问题。我会接受你的答案,并可能开始一个新的。谢谢大家!@穆利·尤扎里