zoukankan      html  css  js  c++  java
  • angularJs 自定义服务 provide 与 factory 的区别

    <!DOCTYPE html>
    <html lang="en" ng-app="myApp">
    <head>
        <meta charset="UTF-8" />
        <title>Document</title>
        <script src="angular.min.js" ></script>
        <script type="text/javascript">
        var m1 = angular.module('myApp', []);
    
        // 自定义服务 -- factory
        // m1.factory('myService', function() {
        //     return {
        //         name : 'liuyi',
        //         age : 30,
        //         showName : function() {
        //             return this.name + '今年' + this.age + '岁了'; 
        //         }
        //     };
    
        // });
        
        // 自定义服务 provider
        m1.provider('myService', function() {
            return {
                name : '刘二',
                age : 40,
                $get : function() {
                    return {
                        name : this.name,
                        age : this.age,
                        showName : function() {
                            return this.name + '今年' + this.age + '岁了'; 
                        } 
    
                    };
                }            
    
            };
        });
        
        // 自定义服务 -- 随机函数
        // m1.factory('rndFn', function() {
    
        //     return function( n1, n2 ) {
        //         return Math.random()*(n2 -n1) + n1;
        //     }
        // });
    
        // 改写配置参数
        // m1.config( ['myServiceProvider', function(myServiceProvider) {
    
        //     myServiceProvider.age = 100;
    
        // }] );
        m1.config( ['randomFnProvider', function(randomFnProvider) {
    
            randomFnProvider.bInt = false;
    
        }] );
        // 控制器
        // m1.controller('firstController', ['$scope', 'rndFn', function($scope, rndFn) {
    
        //     console.log( rndFn( 0, 5 ) );
        // }]);
        
        m1.provider('randomFn', function() {
            return {
                bInt : false,
                int : function( args ) {
                    if( args ) {
                        this.bInt = true;
                    } else {
                        this.bInt = false;
                    }
                }, 
                $get : function() {
                    var This = this;
                    return function( n1, n2 ) {
                        return This.bInt ? Math.floor(Math.random()*(n2 - n1) + n1) : Math.random()*(n2 - n1) + n1
                    };
                }
    
            };
    
        });
    
        m1.controller('firstController', ['$scope', 'randomFn', function($scope, randomFn) {
    
            console.log( randomFn(0, 5) );
        }]);
    
    
    
        </script>
    </head>
    <body ng-controller="firstController">
        
    </body>
    </html>
    

      

  • 相关阅读:
    网线帘幕动画
    图片缩放/旋转/平移/设置分辨率
    贝塞尔样条
    线性梯度画刷
    画七彩五角星
    kafka安装
    在windows远程提交任务给Hadoop集群(Hadoop 2.6)
    把Spark SQL的metadata存储到mysql
    使用IDEA开发SPARK提交remote cluster执行
    Netty的Channel
  • 原文地址:https://www.cnblogs.com/zsongs/p/5585887.html
Copyright © 2011-2022 走看看