【发布时间】:2015-10-03 18:28:14
【问题描述】:
我是 Angular 的新手,我不明白为什么我们将它分配给控制器内的变量。
angular.module('NoteWrangler')
.controller('NoteCreateController', function($http){
var controller = this;
this.saveNote = function(note){
controller.errors = null;
$http({method: 'POST', url: '/notes', data: note})
.catch(function(note){
controller.errors = note.data.error;
})
};
});
【问题讨论】:
-
尝试在回调中使用
this.errors....this在那里完全不同。与 Angular 无关,与 javascript 作用域和闭包有关 -
因为
this是函数作用域。因此,如果您想在上层范围内访问this的值,请将其存储在一个变量中。
标签: javascript angularjs ngroute angular-controller