Daniel Montes
Daniel Montes

Reputation: 23

How to deal with Http Promise AngularJS

Im trying to get the data from a response on http promise and set this data on attr from custome diretive, but always get a undefined data on my directive

promiseComboBox.then((response) =>
    {
        $scope.comboBoxTipoParking = response; //get data
    });

module.directive('comboBox', ['$rootScope', function($rootScope){
    return {
        restrict    : 'E',
        scope       : {
            data  : '=',
            label : '='
        },
        templateUrl : '/Scripts/components/select/select.html',
        link        : function(scope, element, attrs)
        {            
            console.log(attrs);//get here but undefined
            scope.label = attrs.label;


            $('.select2').select2({
                width           : '100%',
                dropdownParent  : $('.modalFocusInput')
            });
        }
    }
}]);

<combo-box label="Parqueadero" data="{{comboBoxTipoParking}}">
</combo-box>

//passing the result from http request

Upvotes: 2

Views: 43

Answers (1)

georgeawg
georgeawg

Reputation: 48948

One can use attrs.$observe

app.directive('comboBox', function(){
    return {
        restrict    : 'E',
        scope       : false,
        templateUrl : '/Scripts/components/select/select.html',
        link        : function(scope, element, attrs)
        {            
            console.log(attrs);//get here but undefined
            attrs.$observe("boxData", function(value) {
                console.log(value);
            });
        }     
    }
});
<combo-box label="Parqueadero" box-data="{{comboBoxTipoParking}}">
</combo-box>

Be careful when using the identifier data as an attribute. Directive normalization strips the data- prefix.

Upvotes: 1

Related Questions