anjularjs 第二天

來源:互聯網
上載者:User

標籤:anjularjs

  1.  ng-class 

        專業修改css啊,比css裡加{{}}強多了

        

<p ng-class=‘{right:attack,wrong:cure}‘>100</p>

<button ng-click="attack=true;cure=falue">attack</button>

<button ng-click="attack=false;cure=true">cure</button>


        點哪行哪行變綠

            650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/4D/BF/wKiom1RYxUHgvZMEAABfVczNPsA696.jpg" title="未命名.JPG" alt="wKiom1RYxUHgvZMEAABfVczNPsA696.jpg" />


 <div ng-controller=‘ListController‘>

<table border="1">

    <tr ng-class="{highlight:bc == $index}" ng-repeat="room in directory" ng-click="select($index)">

<td>{{room.name}}</td>

<td>{{room.cuisine}}</td>

</tr>

</table>

</div>

<script>

function ListController($scope) {

$scope.directory = [{name:‘The Handsome Heifer‘, cuisine:‘BBQ‘},

{name:‘Greens Green Greens‘,cuisine:‘Salads‘},

{name:‘House of Fine Fish‘, cuisine:‘Seafood‘}];

$scope.select = function(row){

$scope.bc = row;

}

}



2.<img ng-src> <a ng-href>


Using Angular markup like {{hash}} in a src attribute doesn‘t work right: The browser will fetch from the URL with the literal text {{hash}} until Angular replaces the expression inside {{hash}}. The ngSrcdirective solves this problem.

The buggy way to write it:

<img src="http://www.gravatar.com/avatar/{{hash}}"/>

The correct way to write it:

<img ng-src="http://www.gravatar.com/avatar/{{hash}}"/>




3.ng-control 嵌套規則

<div ng-controller="ParentController">

  <div ng-controller="ChildController">...</div>

</div>

雖然我們把這種方式叫做控制器嵌套,但真實的嵌套發生在 $scope對象上。通過內部

的原型繼承機制,父控制器對象上的 $scope會被傳遞給內部嵌套控制器的 $scope。具

體到上面例子就是,ChildController 的 $scope對象可以訪問 ParentController 的

$scope對象上的所有屬性(和函數) 。


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/4D/BF/wKioL1RYyVrwO5ERAAP9CAhKAmU284.jpg" title="未命名.JPG" alt="wKioL1RYyVrwO5ERAAP9CAhKAmU284.jpg" />













4.購物車,總價大於100時,減10元

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4D/C1/wKiom1RY2f2C0AlAAABkHtcy0mc105.jpg" title="未命名.JPG" alt="wKiom1RY2f2C0AlAAABkHtcy0mc105.jpg" />

<body>

<div ng-controller="busContr">

<table border="1">

<tr >

<th>編號</th>

<th>單價</th>

<th>數量</th>

<th>總價</th>

</tr>

<tr ng-repeat="item in items">

<td>{{$index}}</td>

<td>{{item.price}}</td>

<td><input type="text" ng-model="item.number"></input></td>

<td>{{item.price*item.number}}</td>

</tr>

<tr>

<td></td>

<td></td>

<td>total</td>

<td>{{total()}}</td>

</tr>

<tr>

<td></td>

<td></td>

<td>discount</td>

<td>{{discount}}</td>

</tr>

</table>

</div>

<script>

function busContr($scope){

var items = [{price:12,number:0},

{price:125,number:0},

{price:1,number:0}];

$scope.items = items;

$scope.total = function(){

var all = 0;

for(var i=0;i<items.length;i++){

all += $scope.items[i].price * $scope.items[i].number;

}

return all;

}

function decrease(){

if($scope.total() > 100)

$scope.discount = 10;

else

$scope.discount = 0;

}

$scope.$watch($scope.total,decrease);

}

</script>


</body>

$watch函數既可以接受一個函數(就像我們前面做的) , 也可以接受一個字串。

如果把一個字串傳遞給了 $watch函數,它將會在被調用的 $scope範圍中當成表達

式來執行。


如果你想監控多個屬性或者對象,並且當其中任何一個發生變化時就去執行一個函數,

應該怎麼做呢?你有兩種基本的選擇 :

監控把這些屬性串連起來之後的值。 

把它們放到一個數組或者對象中,然後給deepWatch參數傳遞一個 true 值。




5.使用服務factory


When you first get started with Angular, you’ll naturally find yourself flooding your controllers and scopes with unnecessary logic. It’s important to realize early on that your controller should be very thin; meaning, most of the business logic and persistent data in your application should be taken care of or stored in a service. I see a few questions a day on Stack Overflow regarding someone trying to have persistent data in his or her controller. That’s just not the purpose of a controller. For memory purposes, controllers are instantiated only when they are needed and discarded when they are not. Because of this, every time you switch a route or reload a page, Angular cleans up the current controller. Services however provide a means for keeping data around for the lifetime of an application while they also can be used across different controllers in a consistent manner.

 

Angular provides us with three ways to create and register our own service.

 

1) Factory

2) Service

3) Provider

 

Factory

1) When you’re using a Factory you create an object, add properties to it, then return that same object. When you pass this service into your controller, those properties on the object will now be available in that controller through your factory.

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/4D/C0/wKioL1RY5uqgcZcLAAIWRtoYrHY000.jpg" title="未命名.JPG" alt="wKioL1RY5uqgcZcLAAIWRtoYrHY000.jpg" />














anjularjs 第二天

聯繫我們

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