factory方法返回的是对象,json或数组,也可以返回字符串类型的数据,但service方法只能返回数据或对象

创建服务有3种方法

$provide.provider('服务名',function(){this.$get=function(){return obj}})

$provide.factory('服务名',fn);$provide.service('服务名',fn)

 

服务的作用是用来在多个控制器内共享数据

angular.module('myApp',[]).factory('服务名',function(){

  return {uname:'kevin',pwd:'123'}

}).controller('ctrlname',function('服务名',$scope){

}.directive('myDirective', function(){


        template: '<button>Click me</button>'
    }
})

 
                    
            
                

相关文章:

  • 2021-12-12
  • 2021-06-23
  • 2021-09-30
  • 2021-06-26
  • 2021-08-31
猜你喜欢
  • 2022-12-23
  • 2021-05-26
  • 2021-07-06
  • 2022-12-23
  • 2022-01-17
  • 2022-12-23
  • 2021-11-03
相关资源
相似解决方案