AngularJS Foundations

Controller and $Scope

$scope is the glue between the Controller and the View The Controller is responsible for constructing the model on $scope and providing commands for the View to act upon $scope provides context

Best Practices

Controllers should...

Not know Anything about the view they control be small and focused

Should not talk to other controllers

Should NOT own the domain model

For Rails Developer

Rails

  • 通过Model获取数据源
  • 实例变量给模板提供数据
  • helper方法放在helpers下面

AngularJS

  • 通过Service获取数据源
  • 模板通过$scope 或者 controller(this)获取数据
  • 通过filter 扩展模板方法