angularjs 自訂服務

來源:互聯網
上載者:User

標籤:

<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>var m1 = angular.module(‘myApp‘,[]);//自訂服務m1.factory(‘myService‘,function(){//服務的名字,回呼函數(跟controller文法一樣,可以是數組)        return {        name : ‘hello‘,        show : function(){            return this.name + ‘:angular‘;        }    };    });m1.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){//使用myService服務(要形參傳進去)    console.log(myService.show());    }]);m1.factory(‘myRandomNum‘,function(){    return function(num1,num2){        return Math.random()*(num2 - num1) + num1;    };    });m1.controller(‘Aaa‘,[‘$scope‘,‘myRandomNum‘,function($scope,myRandomNum){        console.log( myRandomNum(-3,6) );    }]);m1.factory(‘myService‘,function(){        return {        name : ‘hello‘,        show : function(){            return this.name + ‘:angular‘;        }    };    });m1.provider(‘myService‘,function(){        return {        name : ‘hello‘,            $get : function(){                return {                name : this.name,                show : function(){                    return this.name + ‘:angular‘;                }            };                    }    };    });m1.config([‘myServiceProvider‘,function(myServiceProvider){        console.log(myServiceProvider);        myServiceProvider.name = ‘hi‘;    }]);m1.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){        console.log(myService.show());    }]);m1.provider(‘myRandomNum‘,function(){        return {        bolInt : false,        int : function(argBol){            if(argBol){                this.bolInt = true;            }            else{                this.bolInt = false;            }        },        $get : function(){            var This = this;            return function(num1,num2){                return This.bolInt ? Math.round(Math.random()*(num2 - num1)) + num1 : Math.random()*(num2 - num1) + num1;            };        }    };    });m1.config([‘myRandomNumProvider‘,function(myRandomNumProvider){        myRandomNumProvider.int(false);    }]);m1.controller(‘Aaa‘,[‘$scope‘,‘myRandomNum‘,function($scope,myRandomNum){        console.log( myRandomNum(-3,6) );    }]);</script></head><body><div ng-controller="Aaa">   </div></body></html>
<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>var m1 = angular.module(‘module1‘,[]);m1.factory(‘myService‘,function(){    return {        name : ‘hello‘,        show : function(){            return this.name + ‘:angular‘;        }    };});var m2 = angular.module(‘myApp‘,[‘module1‘]);m2.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){    console.log(myService.show());}]);</script></head><body><div ng-controller="Aaa">   </div></body></html>
<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script src="service.js"></script><script>var m2 = angular.module(‘myApp‘,[‘module1‘]);m2.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){        console.log(myService.show());    }]);</script></head><body><div ng-controller="Aaa">   </div></body></html>
<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script src="service.js"></script><script>var m2 = angular.module(‘myApp‘,[‘module1‘]);m1.config([‘myServiceProvider‘,function(myServiceProvider){        myServiceProvider.name = ‘hi‘;    }]);m2.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){        console.log(myService.show());    }]);</script></head><body><div ng-controller="Aaa">   </div></body></html>
<!DOCTYPE HTML><html ng-app="myApp"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>無標題文檔</title><script src="angular.min.js"></script><script>var m1 = angular.module(‘myApp‘,[]);m1.service(‘myService‘,FnService);//FnService是回呼函數(也可以使數組[]),跟controller文法一樣,function FnService(){    this.name = ‘hello‘;}FnService.prototype.age = 20;m1.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){        console.log(myService.name);    console.log(myService.age);    }]);var m1 = angular.module(‘myApp‘,[]);m1.constant(‘myService‘,‘hello angular‘);//m1.value(‘myService‘,‘hello angular‘);m1.config([‘myService‘,function(myService){    console.log(myService);}]);m1.controller(‘Aaa‘,[‘$scope‘,‘myService‘,function($scope,myService){        console.log(myService);    }]);</script></head><body><div ng-controller="Aaa">   </div></body></html>

 

angularjs 自訂服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.