真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

AngularJS(二、如何用AngularJS建立前端代碼框架)

    首先,我們來看一下工程結(jié)構(gòu)目錄:

創(chuàng)新互聯(lián)專注于河源網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗。 熱誠為您提供河源營銷型網(wǎng)站建設(shè),河源網(wǎng)站制作、河源網(wǎng)頁設(shè)計、河源網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造河源網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供河源網(wǎng)站排名全網(wǎng)營銷落地服務(wù)。

AngularJS(二、如何用AngularJS建立前端代碼框架)

    我們可以看到scripts中分列了一下幾個文件夾:controllers、services、directives、filters、vendor,以及兩個文件app.js和main.js。

    controllers里面主要就是控制器

   services里面是服務(wù),為控制器提供數(shù)據(jù)服務(wù)支持,與后臺交互的代碼就在其中

    directives是指令,用于生成自定義標簽的html模板

    filters是過濾器,用于處理顯示數(shù)據(jù)的形式

    vendor文件夾中存放的是引用的第三方j(luò)s插件

跟scripts同級的是styles和views文件夾,styles存放css樣式文件,view存放html文件。

    我們先來看一下index.html中的主要代碼:


 

  My AngularJS App
  

  
  
  





    ……



    
  

我們可以看到導(dǎo)入的js只有一個require.js,但是這個script中有個這樣的屬性 

data-main="scripts/main"

這個屬性就將主js指向了scripts/main.js。

下面來看main.js,其中,分為了兩塊,第一塊是對第三方的引用和依賴配置:

// the app/scripts/main.js file, which defines our RequireJS config
require.config({
    paths:{
        angular:'vendor/angular.min',
        jquery:'vendor/jquery',
        bootstrap:'vendor/bootstrap.min',
        domReady:'vendor/domReady',
        ngGrid:'vendor/ng-grid.debug'
    },
    shim:{
        angular:{
            deps:[ 'jquery'],
            exports:'angular'
        },
        ngGrid:{
            deps:[ 'jquery']
        },
        bootstrap:{
            deps:[ 'jquery']
        }
    }
});

第二塊就是主要的引用和route信息了:

require([
    'angular',
    'app',
    'domReady',
    'bootstrap',
    'services/userService',
    'controllers/rootController',
    'directives/ngbkFocus',
    'controllers/user/listController',
    'controllers/user/editController',
    'controllers/user/newController',
    'ngGrid'
    // Any individual controller, service, directive or filter file
    // that you add will need to be pulled in here.
],
    function (angular, app, domReady) {
        'use strict';
        app.config(['$routeProvider',
            function ($routeProvider) {
                $routeProvider.when('/', {
                    templateUrl:'views/user/list.html',//當跳轉(zhuǎn)到該路徑時,將此html嵌入到ng-view屬性的dom中
                    controller:'ListCtrl'//當前templateUrl的controller為ListCtrl
                })
                    .when('/edit/:name/:allowance', {
                        templateUrl:'views/user/form.html',
                        controller:'EditCtrl'
                    })
                    .when('/new', {
                        templateUrl:'views/user/form.html',
                        controller:'NewCtrl'
                    })
                    .when('/root', {
                        templateUrl:'views/root.html',
                        controller:'RootCtrl'
                    });
            }
        ]);
        domReady(function () {
            angular.bootstrap(document, ['MyApp']);//app名為MyApp

            // The following is required if you want AngularJS Scenario tests to work
            $('html').addClass('ng-app: MyApp');//給html添加ng-app屬性
        });
    }
);

以list的route為示例,我們來講解controller,先看一張list效果圖(此處我們引用了ng-grid插件):

AngularJS(二、如何用AngularJS建立前端代碼框架)我們來看引入的controllers/user/listController:

define(['controllers/controllers', 'services/userService'],
    function (controllers) {
        controllers.controller('ListCtrl', ['$scope', '$http', '$location', 'UserService',//此處數(shù)組中的參數(shù)依次對應(yīng)下面回調(diào)方法中的參數(shù)
            function ($scope, $http, $location, UserService) {
                
                $scope.mySelections = [];
                $scope.filterOptions = {
                    filterText:"",
                    useExternalFilter:true
                };
                $scope.totalServerItems = 0;
                $scope.pagingOptions = {
                    pageSizes:[250, 500, 1000],
                    pageSize:250,
                    currentPage:1
                };
                $scope.setPagingData = function (data, page, pageSize) {
                    var pagedData = data.slice((page - 1) * pageSize, page * pageSize);
                    $scope.myData = pagedData;
                    $scope.totalServerItems = data.length;
                    if (!$scope.$$phase) {
                        $scope.$apply();
                    }
                };
                $scope.getPagedDataAsync = function (pageSize, page, searchText) {
                    setTimeout(function () {
                        var data;
                        if (searchText) {
                            var ft = searchText.toLowerCase();
                            UserService.getServerData(function (largeLoad) {
                                data = largeLoad.filter(function (item) {
                                    return JSON.stringify(item).toLowerCase().indexOf(ft) != -1;
                                });
                                $scope.setPagingData(data, page, pageSize);
                            });
                        } else {
                            UserService.getServerData(function (largeLoad) {
                                $scope.setPagingData(largeLoad, page, pageSize);
                            });
                        }
                    }, 100);
                };

                $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage);

                $scope.$watch('pagingOptions', function (newVal, oldVal) {
                    if (newVal !== oldVal && newVal.currentPage !== oldVal.currentPage) {
                        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                    }
                }, true);
                $scope.$watch('filterOptions', function (newVal, oldVal) {
                    if (newVal !== oldVal) {
                        $scope.getPagedDataAsync($scope.pagingOptions.pageSize, $scope.pagingOptions.currentPage, $scope.filterOptions.filterText);
                    }
                }, true);
                
                $scope.clickme = function (v) {
                    $location.path('/edit/' + v.name + "/" + v.allowance);
                }

                $scope.gridOptions = {
                    data:'myData',
                    enablePaging:true,
                    showFooter:true,
                    totalServerItems:'totalServerItems',
                    pagingOptions:$scope.pagingOptions,
                    filterOptions:$scope.filterOptions,
                    selectedItems:$scope.mySelections,
                    showSelectionCheckbox:true,
                    columnDefs:[
                        { field:"name", pinned:true }
                        ,
                        { field:"allowance", cellTemplate:'{{COL_FIELD | currency}}'}
                        ,
                        { field:"entity", pinned:true, cellTemplate:'{{$parent.row.entity | myfilter}}' }
                        ,
                        { field:"name", width:100, cellTemplate:''}
                    ]/*,
                    multiSelect:false*/

                };
            }]);
    });

其中定義了該controller名為ListCtrl,$scope為局部變量,其中定義的數(shù)據(jù)與方法可在當前Ctrl作用域內(nèi)生效,我們來看一下生效的list.html是怎么樣的

就是這么一句話,定義了grid的div,其余的都交給了controller和ng-grid插件

listController中引用了services/userService,service層用來與后臺交互,管理數(shù)據(jù):

define(['services/services'],
    function (services) {
        services.factory('UserService', ['$http',
            function ($http) {
                return {
                    getData:function () {
                        return [
                            { name:"Moroni", age:50, birthday:"Oct 28, 1970", salary:"60,000" },
                            { name:"Tiancum", age:43, birthday:"Feb 12, 1985", salary:"70,000" },
                            { name:"Jacob", age:27, birthday:"Aug 23, 1983", salary:"50,000" },
                            { name:"Nephi", age:29, birthday:"May 31, 2010", salary:"40,000" },
                            { name:"Enos", age:34, birthday:"Aug 3, 2008", salary:"30,000" },
                            { name:"Moroni", age:50, birthday:"Oct 28, 1970", salary:"60,000" },
                            { name:"Tiancum", age:43, birthday:"Feb 12, 1985", salary:"70,000" },
                            { name:"Jacob", age:27, birthday:"Aug 23, 1983", salary:"40,000" },
                            { name:"Nephi", age:29, birthday:"May 31, 2010", salary:"50,000" },
                            { name:"Enos", age:34, birthday:"Aug 3, 2008", salary:"30,000" },
                            { name:"Moroni", age:50, birthday:"Oct 28, 1970", salary:"60,000" },
                            { name:"Tiancum", age:43, birthday:"Feb 12, 1985", salary:"70,000" },
                            { name:"Jacob", age:27, birthday:"Aug 23, 1983", salary:"40,000" },
                            { name:"Nephi", age:29, birthday:"May 31, 2010", salary:"50,000" },
                            { name:"Enos", age:34, birthday:"Aug 3, 2008", salary:"30,000" }
                        ];
                    },
                    getOptions:function () {
                        return {
                            data:'myData',
                            enablePinning:true,
                            columnDefs:[
                                { field:"name", width:120, pinned:true },
                                { field:"age", width:120 },
                                { field:"birthday", width:120 },
                                { field:"salary", width:120 }
                            ]
                        }
                    },
                    getServerData:function (success) {
                        $http.get('jsonFiles/largeLoad.json').success(success);
                    },

                    saveData:function (data, success) {
                        $http.post('/users/save', data).success(success);
                    }
                };
            }]);
    });

其中引用的$http是用來管理ajax的對象,類似的對象還有$location,用來管理url地址,類似的對象還有很多,詳細可以參見官方API文檔。

    至此,一個前端MVC框架基本構(gòu)建完成,在之后的一篇中我們來著重介紹下還沒講過的filters和directives。

附件:http://down.51cto.com/data/2365610

分享文章:AngularJS(二、如何用AngularJS建立前端代碼框架)
文章來源:http://weahome.cn/article/igdhdj.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部