Javascript 将此变量传递给$http回调
如何将Javascript 将此变量传递给$http回调,javascript,angularjs,Javascript,Angularjs,如何将此传递给angular$http,然后回调,以便使用属性?在没有$scope的情况下可以这样做吗? 这是我的密码: angular.module('app').controller('CollectionCtrl',['$http','$scope', function ($http, $scope) { this.formVisible = false; this.showCollectionForm = function () { this.formVisible = true
此
传递给angular$http,然后
回调,以便使用属性?在没有$scope
的情况下可以这样做吗?
这是我的密码:
angular.module('app').controller('CollectionCtrl',['$http','$scope', function ($http, $scope) {
this.formVisible = false;
this.showCollectionForm = function () {
this.formVisible = true;
};
this.hideCollectionForm = function () {
this.formVisible = false;
};
// New Collection Add
this.collection_name = '';
this.collection_text = '';
this.collection_nameError = false;
this.addCollection = function () {
if (this.collection_name === '') {
this.collection_nameError = true;
}
else {
this.collection_nameError = false;
var url = ROOT_URL + "api/post-image";
var data = {
'collection_name' : this.collection_name,
'collection_text' : this.collection_text
}
$http.post(url, data)
.then(
function(response){
this.hideCollectionForm();
$scope.fetchUserCollection();
},
function(response){
console.log('failure callback');
}
);
}
};
}]));
我无法在
内部调用hideCollectionForm()
,然后
success,因为它未定义。位于控制器顶部
var self = this;
所以self总是指你的控制器,在promise回调中,你可以作为
self.hideCollectionForm();