首页 > 代码库 > provider and $provide.decorator
provider and $provide.decorator
<!DOCTYPE html><html><head> <title></title> <script src="../lib/AngularJs/angular.min.js"></script> <script src="../lib/AngularJs/ui-router.js"></script></head><body> <div data-ng-app="myApp" data-ng-controller="myCtrl"> <h1>home</h1> </div></body><script> var myApp = angular.module(‘myApp‘,[‘ui.router‘]); myApp.provider(‘foo‘,function(){ var thisIsPrivate = ‘private‘; return { setPrivate:function(newVal){ thisIsPrivate = newVal; }, //$get才是公开给用户的 ,其他的可在config里面使用 $get:function(){ function getPrivate(){ return thisIsPrivate; } return { variable:‘public‘, getPrivate:getPrivate }; } }; }); //config用name + Provider , 用户用name// myApp.config(function(fooProvider){// fooProvider.setPrivate(‘new value‘);// }); //添加新属性 myApp.config(function($provide){ $provide.decorator(‘foo‘,function($delegate){ $delegate.greet = function(){ return ‘hello world‘; }; return $delegate;//记得return }); }); myApp.controller(‘myCtrl‘,[‘$scope‘,‘foo‘,function($scope,foo){ $scope.name = ‘Jackey‘; //console.log(myService.getName()); console.log(foo.getPrivate()); }]);</script></html>
provider and $provide.decorator
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。