2016-08-05 8 views
1

Wie kann ich this zu eckigen $http then Callback übergeben, so dass ich Eigenschaften verwenden kann? Ist es möglich, das ohne $scope zu tun? Hier ist mein Code:

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'); 
        } 
       ); 
    } 
}; 

}]); Ich kann hideCollectionForm() innerhalb then Erfolg nicht aufrufen, weil es undefiniert ist.

Antwort

2

Auf der Oberseite des Controllers;

var self = this; 

So Selbst bezieht sich immer auf die Steuerung und in dem Versprechen Rückruf Sie verwenden CA,

self.hideCollectionForm();