angularJS(5)

來源:互聯網
上載者:User

標籤:分享   資料   image   name   部分   control   bsp   tool   dom   

一,資料迴圈:特別要注意範圍

使用ng-repeat指令。

<div ng-app="myApp" ng-controller="myCtrl">    <ul>        <li ng-repeat="x in names">{{x}}</li>    </ul></div><script>var app = angular.module(‘myApp‘, []);app.controller(‘myCtrl‘, function($scope) {    $scope.names=["金角大王","銀角大王","紅孩兒"];});</script>

二:angularJs控制器

1.AngularJS 應用程式被控制器控制。
2.ng-controller 指令定義了應用程式控制器。

控制器使用方法:

<div ng-app="myapp" ng-controller="myctrl">    姓:<input type="text" ng-model="xing"><br>        名:<input type="text" ng-model="ming">        <h1>你的姓名是:{{xing+""+ming}}</h1>    <h2>好孩子:{{aaa()}}</h2></div><script>    var app=angular.module("myapp",[]);    app.controller("myctrl",function($scope){         $scope.xing="孫";         $scope.ming="悟空";         $scope.aaa=function(){            return $scope.xing+$scope.ming+“喜歡吃香蕉";        }    })</script>

接下來咱來解析一下這個案例:

AngularJS 應用程式由 ng-app 定義。應用程式在 <div> 內運行。
AngularJS 指令用於定義一個控制器:ng-controller="myctrl" 
myctrl 函數是一個 JavaScript 函數。
AngularJS 使用$scope 對象來調用控制器。
在 AngularJS 中, $scope 是一個應用對象(屬於應用變數和函數)。
控制器的 $scope (相當於範圍、控制範圍)用來儲存AngularJS Model(模型)的對象。
控制器在範圍中建立了兩個屬性 (xing 和 ming)。
ng-model 指令綁定輸入欄位到控制器的屬性(xing 和 ming).

 

三,DOM指令。

1,ng-disabled指令:直接繫結應用程式資料到HTML的disabled屬性。

<body ng-app="myapp" ng-controller="mctrl">        <input type="button" value="下一步" ng-disabled="!mm"/>        <input type="checkbox" ng-model="mm"/>            </body>        <script type="text/javascript">            var app=angular.module(‘myapp‘,[]);            app.controller(‘mctrl‘,function ($scope) {});        </script>

2,ng-hide指令(隱藏元素)

<body ng-app="myapp" ng-controller="myctrl">        <input type="checkbox" ng-model="mm" />隱藏        <div ng-hide="mm">            <h4>八百標兵奔北坡</h4>        </div>    </body>    <script type="text/javascript">        var app=angular.module(‘myapp‘,[]);        app.controller(‘myctrl‘,function ($scope) {})    </script>

3,ng-show指令:根據value的值來顯示(隱藏)HTML元素;可以使用運算式來計算布爾值(true或false)。

<body ng-app="myapp" ng-controller="myctrl">        <input type="checkbox"  ng-model="mm"/>顯示        <div id="" ng-show="mm">            <h2>窗前明月光</h2>        </div>    </body>    <script type="text/javascript">        var app=angular.module(‘myapp‘,[]);        app.controller(‘myctrl‘,function ($scope) {})    </script>

4,angularJs顯示隱藏

(1)ng-hide 指令用於設定應用部分是否可見。
         ng-hide="true" 設定 HTML 元素不可見。
         ng-hide=“false” 設定 HTML 元素可見。
(2)ng-show 指令可用於設定應用中的一部分是否可見 。
       ng-show="false" 可以設定 HTML 元素 不可見。 
       ng-show="true" 可以以設定  HTML 元素可見。

 轉

angularJS(5)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.