我最近開始和AngularJS一起工作,掌握了基礎知識,但在我看到的例子中,我找到了兩種不同的方法。
使得變量和函數私營:
function myController(){
var controller = this;
controller.myVar = 0;
controller.myFunction = function(){...};
}
,使他們的公共通過$範圍:
function myController(){
$scope.myVar = myVar;
$scope.myFunction = function(){...}
}
我可以理解,需要做一些私有的,但暴露的變量和函數因爲在這些情況下,感覺像單一責任原則被打破,公衆並不認同我。
有沒有很好的理由讓這樣的公開?有沒有其他方法可以公開某些東西?還是我擔心太多?
當你想使用雙向數據綁定時,使用'$ scope'。即當您不想顯式讀取/更新DOM時。 – Tushar
最好的信息在這裏http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers –