AngularJS 的controller其實就是一個方法,它有三種寫法:
第一種:
- <pre name="code" class="javascript">var AppController = ['$scope', function($scope){
- $scope.notifyServiceOnChage = function(){
- console.log($scope.windowHeight);
- };
- }];
- app.controller('AppController',AppController);
在定義AppController的時候,先聲明方法需要注入的參數,然后再定義方法體。最后將AppController綁定到app上。
第二種:
- app.controller('AppController', function($scope){
- $scope.notifyServiceOnChage = function(){
- console.log($scope.windowHeight);
- };
- })
直接在app的controller屬性定義,首先是controller名字,然后是方法體。
第三種:
- function AppController($scope) {
- $scope.notifyServiceOnChage = function(){
- console.log($scope.windowHeight);
- };
- }
直接寫方法,然后在ng-controller引用該方法