AngularJS

AngularJS簡介

        AngularJS 誕生於2009年,由Misko Hevery 等人創建,後爲Google所收購。是一款優秀的前端JS框架,已經被用於Google的多款產品當中。

AngularJS四大特徵

1:MVC 模式
AngularJS

  • Model:數據,其實就是angular變量($scope.XX);
  • View: 數據的呈現,Html+Directive(指令);
  • Controller:操作數據,就是function,數據的增刪改查;

2:雙向綁定
AngularJS
3:依賴注入
        依賴注入(Dependency Injection,簡稱DI)是一種設計模式, 指某個對象依賴的其他對象無需手工創建,只需要“吼一嗓子”,則此對象在創建時,其依賴的對象由框架來自動創建並注入進來,其實就是最少知識法則。

4:模塊化設計
   高內聚低耦合法則
        1)官方提供的模塊:ng、ngRoute(路由)、ngAnimate(動畫)
        2)用戶自定義的模塊:angular.module('模塊名',[ ])

案例

<html>
<head>
    <title>angularJS入門案例</title>
    <script src="angular.min.js"></script>
    <script>
        //建立模塊
        var app = angular.module("myApp",[]);
        //創建控制器,$scope就是控制層和視圖層之間交換數據的橋樑
        /*
        app.controller("myController",function($scope){
            $scope.add = function(){
                return parseInt($scope.x)+parseInt($scope.y);
            }
        });
        */
        /*
        app.controller("myController",function($scope){
            $scope.add = function(){
                $scope.z = parseInt($scope.x)+parseInt($scope.y);
            }
        });
        */
        app.controller("myController",function($scope){
//          $scope.array = [111,22,33,44,55];
//          $scope.list = [
//              {name:"張三",shuxue:100,yuwen:100},
//              {name:"李四",shuxue:10,yuwen:40},
//              {name:"王五",shuxue:10,yuwen:10}
//          ];
            //8:內置服務,$http
            $.scope.findList = function(){
                $http.get("data.json").success(
                    function(response){
                        $scope.list = response;
                    }
                );
            }
        });
    </script>
</head>
<body ng-app="myApp" ng-controller="myController" ng-init="findList()">
    第一個數:<input ng-model="x"/> &nbsp;第二個數:<input ng-model="y"/>
    <!--
    <h3>4:控制器</h3>
    第一個數:<input ng-model="x"/> &nbsp;第二個數:<input ng-model="y"/>
    {{add()}}
    -->
    <!--
    <h3>5:事件指令</h3>
    <button ng-click="add()">運算</button>
    運算結果:{{z}}
    -->
    <h3>6:循環數組</h3>
    <table>
        <tr ng-repeat="x in array">
            <td>{{x}}</td>
        </tr>
    </table>
    <h3>7:循環對象數組</h3>
    <table>
        <tr>
            <td>姓名</td>
            <td>數學</td>
            <td>語文</td>
        </tr>
        <tr ng-repeat="x in list">
            <td>{{x.name}}</td>
            <td>{{x.shuxue}}</td>
            <td>{{x.yuwen}}</td>
        </tr>
    </table>
</body>
<!--
<body ng-app ng-init="name='xiaoming'">
    ng-app是angular的啓動引擎<br/>
    <h3>1:表達式</h3>
    {{100+100}}<br/>
    <h3>2:雙向綁定</h3>
    請輸入姓名:
    <input ng-model="name">&nbsp;
    <input ng-model="name">&nbsp;
    {{name}}<br/>
    <h3>3:初始化</h3>
    ng-init="name='xiaoming'"
</body>
-->
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章