1. 程式人生 > >ng-class、ng-style、ng-href、ng-attr-title

ng-class、ng-style、ng-href、ng-attr-title

在angularjs中,設定樣式:

   <style>
        .red{
            background: red;
        }
        .yellow{
            background: yellow;
        }

        div a {
            text-decoration: none;
        }

    </style>
    <script src="js/angular.min.js"></script>
    <script>
        var app = angular.module("myApp",[]);
        app.controller("myCtrl", function($scope){
            $scope.text = "hello";
            $scope.sClass = "{red:true}";
            //$scope.url = "http://www.baidu.com";
            $scope.url = "http://www.baidu.com";
        })
    </script>
<div ng-controller="myCtrl">
        <div ng-class="{{sClass}}">{{text}}</div>
        <div ng-class="{red:true,yellow:true}">{{text}}</div>
        <div ng-style="{background:'red',color:'white'}">{{text}}</div>
        <div>
            <a ng-href="{{url}}">aaaaa</a><br/>
            <a ng-attr-href="{{url}}" ng-attr-title="{{text}}" ng-attr-class="" ng-attr-style="">aaaaa</a>
        </div>
    </div>
樣式如下:


點選aaa,可連結到百度;把滑鼠箭頭放到第二個aaa上,會出現標題“hello”;

注意:在angular中用ng-class設定內部樣式時,樣式格式為:  如:ng-class="{red:true}",後面有個true;

外部樣式與class類似;

用ng-style設定樣式時,與style沒什麼大的區別