码迷,mamicode.com
首页 > 其他好文 > 详细

angluar 区分service/factory/provider 的“hello world”版

时间:2015-04-28 12:17:58      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

var myApp = angular.module("myApp",[]);

// controller 中引用 provider factory service 的时候,不需要添加后缀。。。
myApp.controller("myController",function($scope,my,myFactory,myService){
    $scope.hellos = [
        my.sayHello(),
        myFactory.sayHello(),
        myService.sayHello()
    ]

});

myApp.service("myService",function(){
    // 注意this 对象
    this.sayHello = function () {
        return "hello world for service";
    }
});


// 注意 注意使用$get 方法 
myApp.provider("my", function () {
    this.name = "default";

    this.$get = function () {
        var name = this.name;
        return {
            sayHello: function () {
                return "hello " + name+ " for provider" ;
            }
        }
    }

    this.setName = function(name){
        this.name = name;
    }
});

// 注意 return
// var xxx = {} ;
// return xxx; 
myApp.factory("myFactory", function () {
    return{
        sayHello: function () {
            return "hello world for factory";
        }
    }
});

// 对于 provider 必须添加 "provider"后缀
myApp.config(function (myProvider) {
    myProvider.setName("world");
})






angluar 区分service/factory/provider 的“hello world”版

标签:

原文地址:http://my.oschina.net/bosscheng/blog/406974

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!