angular的学习

第一天从公司的后台,转到前端框架的使用。


1.service代码
Home.$inject = ['Ajax'];

function Home(Ajax) {

    return {
        getHotGoodsList: getHotGoodsList,getCategoryTree: getCategoryTree,getGoodsByCategory1List: getGoodsByCategory1List,};

    function getHotGoodsList(vm) {
        return Ajax.doPost(
            '/mall/goods/list_hot',{
                maxNumber: vm.maxNumber,deviceType: vm.deviceType,}
        );
    }

    function getCategoryTree() {
        return Ajax.doPost(
            '/system/category/get_tree',{}
        );
    }

    function getGoodsByCategory1List(deviceType,page,pageSize) {
        return Ajax.doPost(
            '/mall/goods/list_by_all_categories',{
                deviceType: deviceType,page: page,pageSize: pageSize,}
        );
    }
}
/**  * 首页控制器  */  HomeCtrl.$inject = ['Home','Cache','DEVICE_TYPE'];

function HomeCtrl(Home,Cache,DEVICE_TYPE) {
    var vm = this;

    vm.maxNumber = 4;
    vm.deviceType = DEVICE_TYPE.MOBILE;
    vm.page = 1;
    vm.pageSize = 4;

    vm.hotGoodsList = [];
    vm.categoryTree = [];
    vm.goodsByCategory1List = [];

    _init();

    function _init() {
        _initHotGoodsList();
        _initCategoryTree();
        _initGoodsByCategory1List();
    }

    function _initHotGoodsList() {
        vm.hotGoodsList = Cache.getHotGoodsList();
        if(vm.hotGoodsList === null) {
            Home
                .getHotGoodsList(vm)
                .success(function(response) {
                    if(response.status === 0) {
                        vm.hotGoodsList = response.data;
                        Cache.setHotGoodsList(vm.hotGoodsList);
                    }
                });
        }
    }

    function _initCategoryTree() {
        vm.categoryTree = Cache.getCategoryTree();
        if(vm.categoryTree === null) {
            Home
                .getCategoryTree()
                .success(function(response) {
                    if(response.status === 0) {
                        vm.categoryTree = response.data;
                        Cache.setCategoryTree(vm.categoryTree);
                    }
                });
        }
    }

    function _initGoodsByCategory1List() {
        vm.goodsByCategory1List = Cache.getGoodsByCategory1List();
        if(vm.goodsByCategory1List === null) {
            //console.log(Home.getGoodsByCategory1List(vm));  Home
                .getGoodsByCategory1List(vm)
                .success(function(response) {
                    if(response.status === 0) {
                        vm.goodsByCategory1List = response.data;
                        Cache.setGoodsByCategory1List(vm.goodsByCategory1List);
                    }
                });
        }
    }
}

/**
 * 这是app.controller.js文件  * 所有controller都加到这里
 * 相当于声明控制器  */ angular
    .module('mobileMall')
    .controller('MainCtrl',MainCtrl)
    .controller('HomeCtrl',0)">HomeCtrl)
    .controller('CategoryCtrl',0)">CategoryCtrl)
    .controller('CartCtrl',0)">CartCtrl);

MainCtrl.$inject = ['$rootScope','$scope'];

function MainCtrl($rootScope,$scope) {
    var vm = this;
}

//这是app.module.js文件夹,这里把一些需要的东西注入进去,也可以声明全局量
angular
    .module('mobileMall',[
        'ui.router','ngSanitize','oc.lazyLoad','ngAnimate'
    ])
    .constant('DEVICE_TYPE',{
        MOBILE: 2,PC: 1
    });

/**  * 所有service都加到这里  * 这是app.service.js文件,这里其实是一些全局或者共用的service  */  'mobileMall')
    .factory('Goods',0)">Goods)
    .'Ajax',0)">Ajax)
    .'Cache',0)">Cache)
    .'Validator',0)">Validator)
    .'Home',0)">Home)
    .'Category',0)">Category)
    .'Cart',0)">Cart);

Ajax.'$http'Ajax($http) 
prefix = '/ajax';

    return         doPost: doPost
    };

    doPost(url,jsonParams)         return $http.post(
            + url,0)">_param(jsonParams),                headers:                     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                }
            }
        );
    _param(jsonParams) str [];
        for (key in jsonParams)             str.push(encodeURIComponent(key) + '=' + encodeURIComponent(jsonParams[key]));
        return str.join('&');
    
Validator()     判断手机号码是否合法 *  * @param string str [description] @return boolean [description]  isValidMobile(str) return (/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/).test(str);
    
Cache.[];

Cache() hotGoodsList null;
    categoryTree goodsByCategory1List null;

            getHotGoodsList: function()             hotGoodsList;
        },setHotGoodsListfunction(list) = list;
        categoryTree;
        goodsByCategory1List;
            }
}

相关文章

AngularJS 是一个JavaScript 框架。它可通过 注:建议把脚本放在 元素的底部。这会提高网页加载速度,因...
angluarjs中页面初始化的时候会出现语法{{}}在页面中问题,也即是页面闪烁问题。出现这个的原因是:由于...
AngularJS 通过被称为指令的新属性来扩展 HTML。AngularJS 指令AngularJS 指令是扩展的 HTML 属性,带有...
AngularJS 使用表达式把数据绑定到 HTML。AngularJS 表达式AngularJS 表达式写在双大括号内:{{ expres...
ng-repeat 指令可以完美的显示表格。在表格中显示数据 {{ x.Name }} {{ x.Country }} 使用 CSS 样式为了...
$http是 AngularJS 中的一个核心服务,用于读取远程服务器的数据。读取 JSON 文件下是存储在web服务器上...