วันเสาร์ที่ 28 มีนาคม พ.ศ. 2558

Angular js การใช้งาน ng-init

ng-init
เป็นการสั่งให้ ทำก่อนเป็นอันดับหลังจาก load ทุกอย่างเสร็จ
เช่น

<div ng-app="app">
<div ng-controller="FirstController" ng-init="Init()" >
<div>
</div>

<script>
var app = angular.module("app",[]);
app.controller('FirstController',function($scope)
{
   $scope.Init = function(){
      alert("Test");
   }
});
</script>

เมื่อรัน page เสร็จ angular จะเริ่มสั่งให้ function  Init() ทำงานโดยการแสดง alert ที่แสดงคำว่า "Test" ให้เราเห็นครับ