Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将此变量传递给$http回调_Javascript_Angularjs - Fatal编程技术网

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();