侧边栏壁纸
博主头像
落叶人生博主等级

走进秋风,寻找秋天的落叶

  • 累计撰写 130562 篇文章
  • 累计创建 28 个标签
  • 累计收到 9 条评论
标签搜索

目 录CONTENT

文章目录

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

2023-12-18 星期一 / 0 评论 / 0 点赞 / 29 阅读 / 2547 字

varmyApp=angular.module("myApp",[]);//controller中引用providerfactoryservice的时候,不需要添加后缀。。。myApp.control

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");})






广告 广告

评论区