angularjs1.x.x基礎(chǔ)案例

說明:本案例將教會你怎么注冊module赔桌,以及綁定controller供炎,注冊component,注冊factory疾党,service以及provider.

1. 創(chuàng)建一個模塊以及注冊控制器

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myController as ctrl" >
        msg:{{ctrl.msg}}                
    </div>                                                                          
    <script type="text/javascript" >        
        angular.module("myApp", []).controller("myController", function($scope) {
            this.msg = "hello, myApp is created by angular";
        });                                                                                         
    </script>
</body>
</html>

說明:這里用到了module以及controller音诫。

2. 注冊組件并傳參到組件

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myController as ctrl" >
        msg:{{ctrl.msg}}                
        <my-component msg="ctrl.msg"></my-componet1>        
    </div>                                                                          
    <script type="text/javascript" >        
        angular.module("myApp", []).controller("myController", function($scope) {
            this.msg = "hello, myApp is created by angular";
        });                                                                                                     
            
        angular.module("myApp").component("myComponent",  {         
            template: `                                     
                myComponent1:{{$ctrl.msg}},<br/>                    
                `,                                                          
            controller: ['$scope', function(){}],           
            bindings: { 
                msg: "<"    
            }                           
        });             
    </script>
</body>
</html>

說明:這里用到了component組件注冊并調(diào)用。

3. 工廠服務(wù)創(chuàng)建并使用

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myController as ctrl" >
        msg:{{ctrl.msg}}    
        <br/>       
        <my-component msg="ctrl.msg"></my-componet1>        
    </div>                                                                              
    <script type="text/javascript" >        
        angular.module("myApp", []).factory("MyFactory", [function() {
            return {
                text: "I am factory intance"
            };  
        }]);            
        angular.module("myApp").controller("myController", function($scope, MyFactory) {
            this.msg = MyFactory.text;  
        });                                                                                             
    </script>
</body>
</html>

4. service服務(wù)創(chuàng)建并使用

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myController1 as ctrl" >
        msg:{{ctrl.msg}}    
    </div>
    <div ng-controller="myController2 as ctrl" >
        msg:{{ctrl.msg}}    
    </div>  
    <script type="text/javascript" >            
        angular.module("myApp", []).service("MyService", function() {
            this.text = "I am service instance";        
        });                         
        angular.module("myApp").controller("myController1", function($scope, MyService) {
            this.msg = MyService.text;  
            MyService.text = "changed by myController1";
        });         

        angular.module("myApp").controller("myController2", function($scope, MyService) {
            this.msg = MyService.text;  
        });                                                                                                     
    </script>
</body> 
</html>

可以看出仿贬,在myController1中改變了text的值纽竣,在myController2已經(jīng)體現(xiàn)出來了。

5.provider的注冊并使用

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">
    <div ng-controller="myController1 as ctrl" >
        msg:{{ctrl.msg}}    
    </div>
    <div ng-controller="myController2 as ctrl" >
        msg:{{ctrl.msg}}    
    </div>      
    <script type="text/javascript" >            
        angular.module("myApp", []).provider("MyProvider", function() {
            this.text = "I am provider instance";   

            this.$get = function() {
                return this;
            }           
        });     
        angular.module("myApp").config(function(MyProviderProvider) {
            MyProviderProvider.text += " changed";
        });                                     
        angular.module("myApp").controller("myController1", function($scope, MyProvider) {
            this.msg = MyProvider.text;     
            MyProvider.text = "changed by myController1";
        });                                     

        angular.module("myApp").controller("myController2", function($scope, MyProvider) {
            this.msg = MyProvider.text;         
        });                                                                                     
    </script>       
</body> 
</html> 

5.directive的使用

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="https://cdn.bootcss.com/angular.js/1.5.8/angular.js"></script>
</head>
<body ng-app="myApp">   
    <div ng-controller="MyController as ctrl">
        <p >{{msg}}</p>
        <my-directive ></my-directive> 
    </div>                                                           
    <script type="text/javascript" >        
        var app = angular.module("myApp", []);  
        app.controller("MyController", function($scope) {
            $scope.msg = "I am a controller";
            $scope.$on("update", function() {
                $scope.msg += " updated";
            });                         
        });     
        app.directive("myDirective", function($compile) {
            return {         
                controller: function($scope) {
                    $scope.clickButton = function() {
                        console.log("I am clicked");
                        $scope.$emit("update", {});
                    }               
                },        
                link: function($scope, el, attrs)  {    
                    el.html(`
                        <div>directive</div>
                        <hr/>       
                        <button ng-click="clickButton($event)">更新父controller.msg</button>
                    `);  

                    $compile(el.contents())($scope);  
                }                   
            }       
        });                                                                                              
    </script>       
</body>     
</html>                                    

說明:上面代碼實現(xiàn)的是動態(tài)生成html并且編譯html為正常的元素以及綁定事件茧泪。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末蜓氨,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子队伟,更是在濱河造成了極大的恐慌穴吹,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,490評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嗜侮,死亡現(xiàn)場離奇詭異港令,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)锈颗,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評論 3 395
  • 文/潘曉璐 我一進(jìn)店門顷霹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人击吱,你說我怎么就攤上這事淋淀。” “怎么了覆醇?”我有些...
    開封第一講書人閱讀 165,830評論 0 356
  • 文/不壞的土叔 我叫張陵朵纷,是天一觀的道長。 經(jīng)常有香客問我永脓,道長袍辞,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評論 1 295
  • 正文 為了忘掉前任常摧,我火速辦了婚禮搅吁,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己似芝,他們只是感情好那婉,可當(dāng)我...
    茶點故事閱讀 67,974評論 6 393
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著党瓮,像睡著了一般详炬。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上寞奸,一...
    開封第一講書人閱讀 51,754評論 1 307
  • 那天呛谜,我揣著相機(jī)與錄音,去河邊找鬼枪萄。 笑死隐岛,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的瓷翻。 我是一名探鬼主播聚凹,決...
    沈念sama閱讀 40,464評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼齐帚!你這毒婦竟也來了妒牙?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,357評論 0 276
  • 序言:老撾萬榮一對情侶失蹤对妄,失蹤者是張志新(化名)和其女友劉穎湘今,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體剪菱,經(jīng)...
    沈念sama閱讀 45,847評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡摩瞎,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,995評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了孝常。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片旗们。...
    茶點故事閱讀 40,137評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖构灸,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情冻押,我是刑警寧澤,帶...
    沈念sama閱讀 35,819評論 5 346
  • 正文 年R本政府宣布盛嘿,位于F島的核電站洛巢,受9級特大地震影響稿茉,放射性物質(zhì)發(fā)生泄漏恃慧。R本人自食惡果不足惜渺蒿,卻給世界環(huán)境...
    茶點故事閱讀 41,482評論 3 331
  • 文/蒙蒙 一痢士、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧茂装,春花似錦怠蹂、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至彼妻,卻和暖如春嫌佑,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背侨歉。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評論 1 272
  • 我被黑心中介騙來泰國打工屋摇, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人为肮。 一個月前我還...
    沈念sama閱讀 48,409評論 3 373
  • 正文 我出身青樓摊册,卻偏偏與公主長得像,于是被迫代替她去往敵國和親颊艳。 傳聞我的和親對象是個殘疾皇子茅特,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,086評論 2 355

推薦閱讀更多精彩內(nèi)容

  • AngularJS是什么?AngularJs(后面就簡稱ng了)是一個用于設(shè)計動態(tài)web應(yīng)用的結(jié)構(gòu)框架棋枕。首先白修,它是...
    200813閱讀 1,607評論 0 3
  • 2.1 Activity 2.1.1 Activity的生命周期全面分析 典型情況下的生命周期:在用戶參與的情況下...
    AndroidMaster閱讀 3,044評論 0 8
  • 0.Android手機(jī)操作系統(tǒng)的四層架構(gòu)? Applications , Application Framewor...
    lucas777閱讀 7,849評論 0 16
  • 今天是悟我之心第二十七天了,因為前些天有點小插曲重斑,導(dǎo)致我沒有按時完成簡書作業(yè)兵睛,感謝曉雨老師和紅姐的理解與包容,感謝...
    蕓倪閱讀 140評論 0 0
  • 春來了,人們都這樣說漾脂。春身著一襲長裙假颇,似神仙妃子,來參加這場四季的宴會骨稿”考Γ可能是冬舍不得離開這錦繡山河姜钳,遲遲...
    雪靈夢閱讀 725評論 2 5