AngularJS实例,Angular,AngularJS 在线

740AngularJS 事件

数字 ++ 事件写在 JS 中:

<div ng-app="myApp" ng-controller="myCtrl">
<!--事件不在行内,写在js中便于后期维护--!>
<button ng-click="sum()">点我!</button>

<p>{{ count }}</p>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.count = 0;
    $scope.sum = function(){
        $scope.count =this.count+1;
    }
});
</script>

尝试一下 »

739AngularJS 事件

绑定切换事件

var app = angular.module('myApp', []);
app.controller('event', ['$scope', function ($scope) {
    $scope.flag = false;
    $scope.text = '点击可见';

    $scope.toggle = function(){
        $scope.flag = !$scope.flag;
        $scope.text = $scope.flag ? '点击消失' : '点击可见';
    }
}])

尝试一下 »

738AngularJS 表格

因跨域问题获取不到值的,可以直接如下赋值。

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    //    $http.get("/try/angularjs/data/Customers_JSON.php")
    //    .then(function(result) {
    //    $scope.names = result.data.records;
    //    });
    $scope.names = [{
        Name: "Alfreds Futterkiste",
        Country: "Germany"
    },
    {
        Name: "Around the Horn",
        Country: "UK"
    },
    {
        Name: "ACentro comercial Moctezuma",
        Country: "Mexico"
    },
    {
        Name: "ABólido Comidas preparadas",
        Country: "Spain"
    }]
});
</script>

737AngularJS 表格

参考案例:

<table>
    <tr style="{{$even?'background-color: #f1f1f1':''}}" ng-repeat="x in names">
        <td>{{ $index + 1 }}</td>
        <td>{{ x.Name }}</td>
        <td>{{ x.Country }}</td>
    </tr>
</table>

尝试一下 »

736AngularJS 表格

使用 $even 和 $odd

参考案例:

<table>
<tr ng-repeat="x in names">
<td style="{{$even?'background-color: #f1f1f1':''}}">{{$index + 1}}</td>
<td style="{{$even?'background-color: #f1f1f1':''}}">{{ x.Name }}</td>
<td style="{{$even?'background-color: #f1f1f1':''}}">{{ x.Country
}}</td>
</tr>
</table>

尝试一下 »