LoginSignup

(一)Trend
(二)Qiita Engineer Festa 2024
(三)Question
(四)Official Event
(五)Official Column
(六)Career
(七)Organization
 



33
Go to list of users who liked
36








More than 5 years have passed since last update.

@Quramy(Yosuke Kurami)

AngularJSService.

JavaScript
Angular
Last updated at Posted at 2014-08-11


""AngularService.

AngularJSService.


myModule.js

var module = angular.module('myModule', []);
module.factory('myService', function(){
  var message = 'Hello, world';
  return {
    hello: function(){
      return message;
    }
  };
});



 hello() ()Service


app.js

var app = angular.module('myApp', ['myModule']);

app.controller('MainCtrl', ['$scope', 'myService', function($scope, myService){
  $scope.message = myService.hello();
}]);



 hello() .

.


Service $provide.provider .
 factory .


myModule.js

var module = angular.module('myModule', []);
module.provider('myService', function(){
  var opt = {message: 'Hello, world'};
  this.configure = function(options){
    opt = angular.extend(opt, options);
  };
  this.$get = function(){
    return {
      hello: function(){
        return opt.message;
      }
    };
  };
});



 provider  this....

provider 使


provider .

 $get (Angular).

$get AngularDI($inject)(factory(...) )


module provider AngularService.
$provide.factory  $provide.value, $provide.service  $provide.provider .

config["()+Provider"] .


app.js

var app = angular.module('myApp', ['myModule']);

app.config(['myServiceProvider', function(myServiceProvider){
  myServiceProvider.configure({
    message: 'My name is Quramy!'
  });
}]);

app.controller('MainCtrl', ['$scope', 'myService', function($scope, myService){
  $scope.message = myService.hello();
}]);



AngularJSService.


Ajax($http)  $httpProvider.interceptor

ngRoute  $routeProvider.when 

etc...



 module.config .

AnguarJS.

Servicemodule.value  module.run .



module.config  module.constant .

$injector  $provide .

Servicemodule.config .



module.config module._invokeQueue .

()`angular.module('ng')._invokeQueueAngularJS.





https://docs.angularjs.org/guide/providers#providers_provider-recipe



33
Go to list of users who liked
36
0
Go to list of comments
Register as a new user and use Qiita more conveniently
(一)You get articles that match your needs
(二)You can efficiently read back useful information
(三)You can use dark theme
What you can do with signing up
Sign upLogin


33
Go to list of users who liked
36

 


How developers code is here.
© 2011-2024Qiita Inc.

Guide & Help
About
Terms
Privacy
Guideline
Design Guideline
Feedback
Help
Advertisement
Contents
Release Note
Official Event
Official Column
Advent Calendar
Qiita Award
API
Career
SNS
X(Twitter)@Qiita
X(Twitter)@qiita_milestone
X(Twitter)@qiitapoi
Facebook@Qiita
Our service
Qiita Team
Qiita Zine
Official Shop
Company
About Us
Careers
Qiita Blog