[AngularJS] 產生ListView並加入Button

  • 160
  • 0
  • 2016-04-12

[AngularJS] 產生ListView並加入Button

JS Bin:http://jsbin.com/pitaga/1/edit?html,output

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="customersCtrl"> 
<ul ng-repeat="x in names">
  <li>
    <label>{{ x.Name }}</label>
    <button type="button" ng-click="myClick(x.Country)">{{x.Country}}</button>
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
    $http.get("http://www.w3schools.com/angular/customers.php")
    .then(function (response) {$scope.names = response.data.records;});
    $scope.myClick = function(x){
      alert(x);
    }
});
</script>

</body>
</html>

 

演示結果

 

點選按鈕後

 

參考:W3Schools http://www.w3schools.com/angular/angular_events.asp

Write By Charley Chang 


新手發文,若有錯誤還請指教,
歡迎留言或Mail✉給我

創用 CC 授權條款


本著作係採用創用 CC 姓名標示-非商業性-相同方式分享 4.0 國際 授權條款授權.