• angularjs笔记(三)


    AngularJS API

    7、其他一些常用指令,布尔类型的指令也可以用表达式

      (1)、数组索引$index

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
       <p ng-repeat="x in names">{{$index+1}}_{{x}}</p>
    </div>
    <script>
        angular.module('myApp', []).controller('myController', function ($scope) {
            $scope.names=['Tom','Lily','John'];
        });
    </script>
    </body>
    </html>
    $index指令

      (2)、ng-disabled对应HTML元素disable属性

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="">
        <!--ng-model绑定的是checkbox的value值
               ng-disabled对应button的disabled属性
               -->
        <input type="checkbox" ng-model="mySwitch"/>
        <button ng-disabled="mySwitch">按钮</button>
    </div>
    </body>
    </html>
    ng-disabled

      (3)、ng-show、ng-hide指令

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="">
        <input type="checkbox" ng-model="mySwitch"/>
        <button ng-show="mySwitch">按钮1</button>
        <button ng-hide="mySwitch">按钮2</button>
        <input type="text" ng-model="hour"/>
        <button ng-hide="hour>10">按钮3</button>
    </div>
    </body>
    </html>
    ng-show、ng-hide

      (4)、ng-click

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        <button ng-click="count=count+1">{{count}}</button>
        <input type="button" ng-click="toggle()" value="toggle"/>
    
        <div ng-show="switch" style="height: 20px; 20px;background-color: beige;border: 1px solid gray">
    
        </div>
    </div>
    <script type="text/javascript">
        angular.module('myApp', []).controller('myController', function ($scope) {
            $scope.count = 0;
            $scope.switch = true;
            $scope.toggle = function () {
                $scope.switch = !$scope.switch;
            };
        });
    </script>
    </body>
    </html>
    ng-click

     (5)、ng-include

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp4" ng-controller="myController4">
      <div ng-include="'/test/angularjsInclude.html'">
    </div>
    </div>
    <script type="text/javascript">
    angular.module('myApp4',[]).controller('myController4',function(){
    
    });
    </script>
    </body>
    </html>
    ng-include
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
    </head>
    <body>
    AngularJSInclude
    </body>
    </html>
    /test/angularjsInclude.html页面

    8、AngularJS依赖注入,value、factory、service、provider、constant五个核心组件作为依赖注入

      (1)、value

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        <input type="text" ng-model="initVal"/>
    </div>
    <script type="text/javascript">
        var myApp=angular.module('myApp',[]);
        myApp.value('initVal',5);
        myApp.controller('myController',function($scope,initVal){
            $scope.initVal=initVal;
        });
    </script>
    </body>
    </html>
    依赖注入value

      (2)、factory

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        initVal1:<input type="text" ng-model="initVal1"/>
        <br/>
        initVal2:<input type="text" ng-model="initVal2"/>
    </div>
    <script type="text/javascript">
        var myApp = angular.module('myApp', []);
        myApp.value('initVal', 5);
        myApp.factory('myFactory', function (initVal) {
            var factory = {};
            factory.multiply1 = function () {
                return initVal * initVal;
            };
            factory.multiply2=function(a,b){
                return a*b;
            };
            return factory;
        });
        myApp.controller('myController', function ($scope, myFactory) {
            $scope.initVal1 = myFactory.multiply1();
            $scope.initVal2=myFactory.multiply2(2,3);
        });
    </script>
    </body>
    </html>
    依赖注入factory

      (3)、service 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        initVal1:<input type="text" ng-model="initVal"/>
    </div>
    <script type="text/javascript">
        var myApp = angular.module('myApp', []);
        myApp.value('initVal', 5);
        myApp.factory('myFactory', function (initVal) {
            var factory = {};
            factory.multiply1 = function () {
                return initVal * initVal;
            };
            factory.multiply2 = function (a, b) {
                return a * b;
            };
            return factory;
        });
        myApp.service('myService', function (initVal, myFactory) {
            this.squre = function (a) {
                return myFactory.multiply2(a, a);
            };
        });
        myApp.controller('myController', function ($scope, myService) {
            console.log(myService);
            $scope.initVal = myService.squre(3);
        });
    </script>
    </body>
    </html>
    依赖注入service

      (4)、provider

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        initVal1:<input type="text" ng-model="initVal"/>
    </div>
    <script type="text/javascript">
        var myApp = angular.module('myApp', []);
        myApp.config(function ($provide) {
            $provide.provider('MyProvideService', function () {
                this.$get = function () {
                    var factory = {};
                    factory.multiply = function (a, b) {
                        return a * b;
                    };
                    return factory;
                };
            });
        });
        myApp.controller('myController', function ($scope, MyProvideService) {
            console.log(MyProvideService);
            $scope.initVal = MyProvideService.multiply(2,3);
        });
    </script>
    </body>
    </html>
    依赖注入provide

      (5)、constant常量

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
    </head>
    <body>
    <div ng-app="myApp" ng-controller="myController">
        initVal1:<input type="text" ng-model="initVal"/>
    </div>
    <script type="text/javascript">
        var myApp = angular.module('myApp', []);
        myApp.constant('myConstant', 23);
        myApp.controller('myController', function ($scope, myConstant) {
            $scope.initVal = myConstant;
        });
    </script>
    </body>
    </html>
    依赖注入constant

    9、AngularJS 路由 

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
        <title></title>
        <meta charset="utf-8">
        <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
        <script src="http://apps.bdimg.com/libs/angular-route/1.3.13/angular-route.js"></script>
    </head>
    <body>
    <div ng-app="myApp">
        <ul>
            <li><a href="#/">首页</a></li>
            <li><a href="#/computers">电脑</a></li>
            <li><a href="#/printers">打印机</a></li>
            <li><a href="#/others">其他</a></li>
        </ul>
        <div ng-view></div>
    </div>
    <script type="text/javascript">
        var myApp=angular.module('myApp',['ngRoute']);
        myApp.config(['$routeProvider',function($routeProvider){
            $routeProvider.when('/',{template:'这是首页'})
                    .when('/computers',{template:'电脑页面'})
                    .when('/printers',{template:'打印机页面'})
                    .otherwise({redirectTo:'/'});
        }]);
    </script>
    </body>
    </html>
    路由简单例子

    依赖angular-route.js脚本

    初始化应用程序时,要导入ngRoute模块

    AngularJS路由URL是通过#+标记实现的,在向服务端发送请求地址时,#符号后面的内容会被浏览器忽略掉

    在HTML中#符号是超链接的锚点,如<a href="#top">置顶</a>,表示跳转到当前页面id或name为top的元素位置处,如果在浏览器地址栏中添加跳转到另一页面的锚点,那么另一页面元素要用id来指定锚点名称,如http://192.168.0.137:8080/anotherPage.html#bottom,会跳转到anotherPage.html页面id为bottom处。注意如果想传参数,要在#符号前传入

    路由到的地址内容显示在ng-view指令标记的元素内

    $routeProvider的两个函数(1)、when有两个参数,第一个是URL正则规则,第二个是路由配置对象(2)、otherwise有一个参数,是路由配置对象

    路由设置对象:

    $routeProvider.when(url, {
        template: string,
        templateUrl: string,
        controller: string, function  array,
        controllerAs: string,
        redirectTo: string, function,
        resolve: object<key, function>
    });

    template:ng-view内插入简单HTML内容

    templateUrl:ng-view内插入HTML模板,如'/test/content.html',会从服务端获取到文件内容插入到ng-view内

    controller:在当前模板上执行controller函数,生成新的$scope

    controllerAs:为controller指定别名

    redirectTo:重新定向到指定地址

    resolve:指定当前controller所依赖的其他模块

  • 相关阅读:
    websocket+nodejs+redis实现消息订阅和发布系统
    nodejs下载图片到本地,根据百度图片查找相应的图片,通过nodejs保存到本地文件夹
    基于vue 2.X和高德地图的vue-amap组件获取经纬度
    获取Class对象的三种方式
    java中的回调机制的理解(小例子)
    URL和URI的区别
    java web开发中各类地址的写法
    ListView性能
    Android中的Handler, Looper, MessageQueue和Thread对应关系
    java几种常用的算法
  • 原文地址:https://www.cnblogs.com/hujiapeng/p/5465189.html
Copyright © 2020-2023  润新知