• AngulatJS factory 使用Module(模块)组织依赖关系


    1 使用Module(模块)组织依赖关系

    <!DOCTYPE html>
    
    <html ng-app="shoppingModule">
    <head>
        <title></title>
        <script src="angular.min.js" type="text/javascript"></script>
        <script>
            var shoppingModule = angular.module("shoppingModule", []);
            shoppingModule.factory("Items", function () {
                var items = {};
                items.query = function () {
              //在服务器中拉取数据
    return [ { name: 'Jackey', age: 25 }, { name: 'Cassi', age: 20 }, { name: 'JC', age: 1.2 } ]; }; return items; }); shoppingModule.controller("shoppingController", function ($scope, Items) { $scope.Items = Items.query(); }); </script> </head> <body> <div ng-controller="shoppingController"> <ul> <li ng-repeat="item in Items"> {{item.name}} </li> </ul> </div> </body> </html>

    需要注意的点是

    1 controller里面

     $scope.Items = Items.query();

    2 factory里面的items.query = function(){};

    2 添加过滤器

    <!DOCTYPE html>
    
    <html ng-app="shoppingModule">
    <head>
        <title></title>
        <script src="angular.min.js" type="text/javascript"></script>
        <script>
            var shoppingModule = angular.module("shoppingModule", []);
            shoppingModule.factory("Items", function () {
                var items = {};
                items.query = function () {
                    return [
                        { name: 'Jackey', age: 25 },
                        { name: 'Cassi', age: 20 },
                        { name: 'uuuuujC', age: 1.2 }
                    ];
                };
                return items;
            });
            //过滤器
            shoppingModule.filter("titleCase", function () {
                var titleCase = function (input) {
                    return input.charAt(0).toUpperCase() + input.slice(1);
                };
                return titleCase;
            });
            shoppingModule.controller("shoppingController", function ($scope, Items) {
                $scope.Items = Items.query();
            });
        </script>
    </head>
    <body>
        <div ng-controller="shoppingController">
            <ul>
                <li ng-repeat="item in Items">
                    {{item.name | titleCase}}
                </li>
            </ul>
        </div>
    </body>
    </html>
  • 相关阅读:
    子树的结点个数
    CF988 D. Points and Powers of Two【hash/数学推理】
    回溯法练习【BFS/DFS】
    Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
    Poj 2947 widget factory (高斯消元解同模方程)
    Poj 2065 SETI (高斯消元)
    Lightoj 1054
    Poj 2528 Mayor's posters (线段树+离散化)
    Lightoj 1090
    Poj 1753 Flip Game 高斯消元
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/3662483.html
Copyright © 2020-2023  润新知