Javascript Angularjs-无参考的注入工厂
我有以下代码: main.jsJavascript Angularjs-无参考的注入工厂,javascript,angularjs,angularjs-service,angularjs-factory,Javascript,Angularjs,Angularjs Service,Angularjs Factory,我有以下代码: main.js angular.controller('myCtrl', function($scope, $rootScope) { $scope.name = "Bob"; } myservices.js angular.factory('myService', function($http) { var myService = { async: function(params) { // $http returns a promise,
angular.controller('myCtrl', function($scope, $rootScope) {
$scope.name = "Bob";
}
myservices.js
angular.factory('myService', function($http) {
var myService = {
async: function(params) {
// $http returns a promise, which has a then function, which also returns a promise
var promise = $http.get("http://myws/user/info/"+params).then(function (response) {
// The then function here is an opportunity to modify the response
console.log(response);
// The return value gets picked up by the then in the controller.
return response.data;
});
// Return the promise to the controller
return promise;
}
};
return myService;
});
如何在
myCtrl
中注入myService
?考虑到它们位于两个独立的文件中。我们需要按顺序添加为运行此文件而创建的所有脚本,请注意顺序:
<script src="angular.js"></script>
<script src="main.js"></script>
<script src="myservices.js"></script>
services.js
应该如下所示:
var app = angular.module("MyModule", []);
app.controller('myCtrl', function($scope, $rootScope, myService) { /* implementation */})
app.factory('myService', function($http) { /* implementation */});
因此,在我们的main.js
中,我们正在创建一个模块来附加我们所有的服务、工厂、提供者、值、常量、控制器和指令。它还允许我们将配置和运行阶段函数放入
模块通过以下方式实例化:
angular.module(“MyModule”,[])代码>
我们提供了其他依赖模块的第二个参数
如果需要,我们可以在使用javascript模块的情况下再次询问angular模块:
angular.module('Foo', []) // declares new module
.factory(..)
angular.module('Bar', ['Foo']) // declares new module
.controller(..) // and imports other module
var-app=angular.module(“MyModule”)代码>我们需要按顺序添加我们为运行此脚本而创建的所有脚本,请注意顺序:
<script src="angular.js"></script>
<script src="main.js"></script>
<script src="myservices.js"></script>
services.js
应该如下所示:
var app = angular.module("MyModule", []);
app.controller('myCtrl', function($scope, $rootScope, myService) { /* implementation */})
app.factory('myService', function($http) { /* implementation */});
因此,在我们的main.js
中,我们正在创建一个模块来附加我们所有的服务、工厂、提供者、值、常量、控制器和指令。它还允许我们将配置和运行阶段函数放入
模块通过以下方式实例化:
angular.module(“MyModule”,[])代码>
我们提供了其他依赖模块的第二个参数
如果需要,我们可以在使用javascript模块的情况下再次询问angular模块:
angular.module('Foo', []) // declares new module
.factory(..)
angular.module('Bar', ['Foo']) // declares new module
.controller(..) // and imports other module
var-app=angular.module(“MyModule”)代码>以下是您需要做的几件事
应该是,它们应该属于相同/不同的角度模块
(如果它是不同的模块,那么您必须将其注入到主模块中才能使用它)
您需要使用angular.module('myApp')
将组件绑定到它,以便该服务在该模块中可用
代码
//app.js
angular.module('myApp', ['myApp.service']); //this should be module definition somewhere
//service.js
angular.module('myApp.service', [])
angular.module('myApp.service').factory('myService', function($http) {
//service code
});
//controller.js
angular.module('myApp').controller('myCtrl', function($scope, $rootScope, myService) {
console.log(myService); //service instance here
$scope.name = "Bob";
}
下面是你需要做的几件事
应该是,它们应该属于相同/不同的角度模块
(如果它是不同的模块,那么您必须将其注入到主模块中才能使用它)
您需要使用angular.module('myApp')
将组件绑定到它,以便该服务在该模块中可用
代码
//app.js
angular.module('myApp', ['myApp.service']); //this should be module definition somewhere
//service.js
angular.module('myApp.service', [])
angular.module('myApp.service').factory('myService', function($http) {
//service code
});
//controller.js
angular.module('myApp').controller('myCtrl', function($scope, $rootScope, myService) {
console.log(myService); //service instance here
$scope.name = "Bob";
}
确保两个文件都已实际加载。如何做到这一点取决于您,可能您正在使用require()
的一些实现,或者您只是将HTML中的所有文件列为
标记
阐明您想要的模块结构。两者应该是同一模块的一部分,还是应该是单独的模块
- 相同模块:一个文件需要声明模块,另一个文件需要扩展模块:
angular.module('Foo', []) // declares new module
.controller(..)
angular.module('Foo') // extends existing module
.factory(..)
- 不同模块:
angular.module('Foo', []) // declares new module
.factory(..)
angular.module('Bar', ['Foo']) // declares new module
.controller(..) // and imports other module
将以下内容注入控制器:
.controller('myCtrl', function ($scope, myService) ..
确保两个文件都已实际加载。如何做到这一点取决于您,可能您正在使用require()
的一些实现,或者您只是将HTML中的所有文件列为
标记
阐明您想要的模块结构。两者应该是同一模块的一部分,还是应该是单独的模块
- 相同模块:一个文件需要声明模块,另一个文件需要扩展模块:
angular.module('Foo', []) // declares new module
.controller(..)
angular.module('Foo') // extends existing module
.factory(..)
- 不同模块:
angular.module('Foo', []) // declares new module
.factory(..)
angular.module('Bar', ['Foo']) // declares new module
.controller(..) // and imports other module
将以下内容注入控制器:
.controller('myCtrl', function ($scope, myService) ..
您可以在控制器中注入服务
比如:
main.js:
angular.module('myApp', []).controller('myController', ['$scope', 'myService',
function ($scope, myService) {
}
]);
myService.js:
angular.module('myApp').factory('myService', function($http) {
//service code
});
对于不同的文件但相同的模块,请确保在使用前加载了该文件 您可以在控制器中注入服务
比如:
main.js:
angular.module('myApp', []).controller('myController', ['$scope', 'myService',
function ($scope, myService) {
}
]);
myService.js:
angular.module('myApp').factory('myService', function($http) {
//service code
});
对于不同的文件但相同的模块,请确保在使用前加载了该文件 如果这些文件在不同的文件中,会有什么不同。只需在注入作用域和根作用域时注入服务。确保您的service.js包含在网页中。如果这些文件位于不同的文件中,会有什么不同。只需在注入作用域和根作用域时注入服务。确保您的service.js包含在网页中。不要使用var-app
。您正在重新声明相同的变量。请不要使用var-app
。您正在重新声明同一个变量