[AngularJS] Provider

Source: Internet
Author: User

This lesson describes what's really happening when you use the angular
Factory and how can make your factories even more dynamic in creation.
This gets further to the internals of AngularJS by showing what a factory is built dynamically for you and how they h Ave reduced the plumbing you need and make applications.

Factory:

<!DOCTYPE HTML><HTML><HeadLang= "en">  <MetaCharSet= "Utf-8">  <title>Egghead Videos</title>  <Linkrel= "stylesheet"href= "Vendor/foundation/foundation.min.css"></Head><Body>  <DivNg-app= "App"Ng-controller= "Appctrl">    <H1class= "Panel">{{title}}</H1>  </Div>  <Scripttype= "Text/javascript"src= "Https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></Script>  <Scripttype= "Text/javascript"src= "App/js/main.js"></Script></Body></HTML>
var app = Angular.module ("app", []); App.factory (function() {    return  {        "StarCraft"    }}) App.controller (function($scope, game) {    = Game.title});

"App.Factory" is just simply short-hand for using this "provide object":

/**/var app = Angular.module ("app", []); $provide) {    $provide. Factory (function() {        return{            " StarCraft "}})    }) App.controller (function($scope, game) {    =  Game.title});

"Factory" is really just shorthand for the provider which sets up a more generic provider which returns things or objects That has the "get" functions:

/** * Created by Answer1215 on 12/27/2014.*/varApp = Angular.module ("app", []); app. Config (function($provide) {$provide. Provider ("Game",function() {        return{$get:function() {                return{title:"StarCraft"}} ) App.controller ("Appctrl",function($scope, game) {$scope. Title=game.title});

If you want to set up something like game provider and set the type of game:

/** * Created by Answer1215 on 12/27/2014.*/varApp = Angular.module ("app", []); App.provider ("Game",function() {    vartype; return{setType:function(title) {type=title; }, $get:function() {            return{Title:type+ "Craft"}}}) app. Config (function(Gameprovider) {Gameprovider.settype ("War");}) App.controller ("Appctrl",function($scope, game) {$scope. Title=game.title});

[AngularJS] Provider

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.