您的位置:首页 > Web前端 > AngularJS

angularjs 的controller的三种写法

2015-11-13 13:48 603 查看
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引用该方法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  angularjs controller