zoukankan      html  css  js  c++  java
  • angularjs服务常见用法

    • 服务可以再应用的整个生命周期内保持数据,不会在切换路由或者重新加载视图时被清楚掉(控制器只有在需要时才被实例化)
    • 服务是一个单例对象,在每个应用中只会被实例化一次
    1. 服务在应用的生命周期内保存数据
    angular.module('myApp')
        .factory('myService', [function(){
        var store_id = null;
    
        function set_curr_storeid(id) {
        store_id = id;
        return;
        }
    
        function get_curr_storeid() {
        return store_id
        }
    
        return {
            'set_curr_storeid': set_curr_storeid,
             'get_curr_storeid': get_curr_storeid   
        }
    }])
    
    //在控制器中使用时 myService.set_curr_storeid(id) 去设置它的值

      2.将一些常用的功能函数封装在服务里,给各个控制器调用

    angular.module('myApp',[])
    .factory('CommonHelper', [function () {
        function get_china_num(status) {
            switch status {
                case 1:
                    return '数字一';
                    break;
                case 2:
                    return '数字二';
                    break;
                default:
                    return '其他';
                    break;
            }
        }
    
        return {
            'get_china_num': get_china_num
        };
    }]);
    
    //在页面中绑定的表达式里直接用服务,类似过滤器的作用
    
    <div ng-repeat = "one_menu in menus">
        <p>{{CommonHelper.get_china_num(one_menu.status)}}</p>
    </div>
  • 相关阅读:
    移动布局---1. 移动端布局基础
    1. CSS新特性之选择器
    1. H5新增语义化标签
    POJ 3281
    poj 1986
    POJ 3728
    poj 2763
    poj 2749
    uva 11294
    LA 3713
  • 原文地址:https://www.cnblogs.com/leijiuling/p/5535456.html
Copyright © 2011-2022 走看看