angularjs基礎教程,angularjs教程

來源:互聯網
上載者:User

angularjs基礎教程,angularjs教程

很久沒有寫過東西了,感覺寫東西都不知道從哪裡開始了,現在還是先寫點技術性的吧,angularjs–我兄弟把它叫成“俺哥啦js”

1.下載

複製代碼 代碼如下:

官方網址:https://angularjs.org/
CDN:https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.5/angular.min.js

2.簡單介紹使用 1.ng-app

決定了angularjs的範圍範圍,你可以如下使用

複製代碼 代碼如下:
<html ng-app>
… 
</html>

來讓angularjs渲染整個頁面,也可以使用

複製代碼 代碼如下:
<div ng-app='myapp'>
……
</div>

來渲染其中的一部分。

2.ng-model

ng-model,當你的資料模型被改變的時候,譬如ng-model='test',其中這個test的數值被改變的時候,{{test}}的數值也將跟隨改變,也就是串連到ng-model中的test也跟隨改變,如下

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app>
    <input ng-model='test' >{{test}}
    </body>
</html>

3.angular.module

這個主要是做模組的註冊,建立和索引,譬如我們ng-app想把這個註冊成為一個服務就要用,當我們引用索引一個模組的時候也要使用

複製代碼 代碼如下:
angular.module(name, [requires], [configFn]);
#name       類型string建立的檢索模組的名稱
#requires   簡單理解就是你需要包含的使用模組,譬如ngRoute路由模組
#configFn   可以選配的功能模組,功能和module.config類似

4.controller

controller是angular.Module中的方法controller(name, constructor);其中name是controller的名字,constructor是控制器建構函式,我們利用一段代碼來說明

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[]);
        app.controller('mytest',function($scope){
            $scope.test="hello word";
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </body>
</html>

5.value

value也是angular.Module中的方法value(name, object);其中name是service的名稱,object是伺服器執行個體對象,這個時候我們就可以把上邊的代碼修改正成這樣

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[])
        .value('testvalue','word');
        app.controller('mytest',function($scope,testvalue){
            $scope.test="hello "+ testvalue;
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </body>
</html>

5.factory

factory也是angular.Module中的方法factory(name, providerFunction);;其中name是service的名稱,providerFunction是函數用於建立新的伺服器對象,這個時候我們就可以把上邊的代碼修改正成這樣

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[])
            .value('testvalue','widuu')
            .factory('testfactory',function(testvalue){
                return{
                    lable:function(){
                        return "this can output : hello "+ testvalue;
                    }
                }
            });
        app.controller('mytest',function($scope,testvalue,testfactory){
            $scope.test = "hello "+ testvalue;
            $scope.output = testfactory.lable();
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </p>
        {{output}}
    </body>
</html>

6.provider

provider也是angular.Module中的方法provider(name, providerType);其中name是service的名稱,providerFunction是函數用於建立新的伺服器對象,這個跟factory差不多,我們現在用provider重寫

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[])
            .value('testvalue','widuu')
            .provider('testprovider',
                function(){
                  this.lable = "this will output : hello widuu";
                  this.$get = function () {
                       return this;
                   }
                }
            );
        app.controller('mytest',function($scope,testvalue,testprovider){
            $scope.test = "hello "+ testvalue;
            $scope.output = testprovider.lable;
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </p>
        {{output}}
    </body>
</html>

7.service

service也是angular.Module中的方法service(name, constructor);其中name是service的名稱,constructor一個將被執行個體化的建構函式,這個跟factory差不多,我們現在用service重寫

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[])
            .value('testvalue','widuu')
            .service('testservice',
                function(testvalue){
                    this.lable = function(){
                        return "this will output:hello "+testvalue;
                    }
                }
            );
        app.controller('mytest',function($scope,testvalue,testservice){
            $scope.test = "hello "+ testvalue;
            $scope.output = testservice.lable();
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </p>
        {{output}}
    </body>
</html>

8.constant

constant也是angular.Module中的方法constant(name, object);其中name是常量的名稱,而object是常量的值,我們可以這樣寫的

複製代碼 代碼如下:
<!doctype html>
<html>
    <head>
        <script src="angular.min.js" type="text/javascript"></script>
        <script type="text/javascript">
        var app = angular.module('myapp',[])
            .value('testvalue','widuu')
            .constant('count',23)
            .service('testservice',
                function(testvalue,count){
                    this.lable = function(){
                        return "this will output:hello "+testvalue+",age is "+count;
                    }
                }
            );
        app.controller('mytest',function($scope,testvalue,testservice){
            $scope.test = "hello "+ testvalue;
            $scope.output = testservice.lable();
        });
        </script>
        <title>learing argularjs--widuu</title>
    </head>
    <body ng-app='myapp' ng-controller='mytest' >
    <input ng-model='test'>{{test}}
    </p>
        {{output}}
    </body>
</html>

今天就寫到這裡,然後以後繼續.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.