Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular directive handles http request

I have angular directive that accept url to obtain remote data:

<my-tag src="http://127.0.0.1/srv1">...

Directive itself:

app.directive('myTag', ['$http', function($http) {
return {
    restrict: 'E',
    transclude: true,
    replace: true,  
    //template: '<div ng-repeat="imgres in gallery">{{imgres.isUrl}}\'/></div>',
    scope:{
        src:"@",            //source AJAX url to dir pictures
    },
    controller:function($scope){
        console.info("enter directive controller");
        $scope.gallery = [];
        $http.get($scope.src).success(function(data){
           console.info("got data");
           $scope.gallery.length = 0;
           $scope.gallery = data;
        });
    }
}

In general it works and I can see in FireBug console:

enter directive controller
GET http://127.0.0.1/srv1
got data

But If I'm placing second instance of directive bind to another url:

<my-tag src="http://127.0.0.1/srv2">...

Works only one with following log:

enter directive controller
GET http://127.0.0.1/srv1
enter directive controller
GET http://127.0.0.1/srv2
got data             <-- as usual it relates to first directive

Couldn't you help me what is wrong with 2 directive nstances

like image 792
Dewfy Avatar asked Mar 03 '14 08:03

Dewfy


People also ask

What is HTTP request in angular?

HttpRequest represents an outgoing request, including URL, method, headers, body, and other request configuration options. Instances should be assumed to be immutable. To modify a HttpRequest , the clone method should be used.

What does $compile do in AngularJS?

Overview. Compiles an HTML string or DOM into a template and produces a template function, which can then be used to link scope and the template together. The compilation is a process of walking the DOM tree and matching DOM elements to directives.

What are the directives of angular?

Directives are classes that add additional behavior to elements in your Angular applications. Use Angular's built-in directives to manage forms, lists, styles, and what users see. See the live example / download example for a working example containing the code snippets in this guide.

What is HTTP get in AngularJS?

AngularJS automatically injects $scope parameter at runtime. The $http. get() method returns HttpPromise which includes methods like success() and error(). The success() method registers a callback method which is called when a request completes successfully.


1 Answers

First of all I don't see any problem. You use directive several times therefore isolate scope is right way.

I just changed src:"@" to src:"=".

Demo Fiddle

HTML

<div ng-controller = "fessCntrl"> 
    <my-tag src="'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, 153 Bukit Batok Street 1&sensor=true'"></my-tag>

    <my-tag src="'http://maps.googleapis.com/maps/api/geocode/json?address=Singapore, SG, Singapore, 3 Bukit Batok Street 1&sensor=true'"></my-tag>        
</div>

JS

app.directive('myTag', ['$http', function($http) {
return {
    restrict: 'E',
    transclude: true,
    replace: true,     
    scope:{
        src:"="       
    },
    controller:function($scope){
        console.info("enter directive controller");
        $scope.gallery = [];

    console.log($scope.src);

        $http({method: 'GET', url:$scope.src}).then(function (result) {
                           console.log(result);                              
                        }, function (result) {
                            alert("Error: No data returned");
                        });
    }
}
}]);
like image 135
Maxim Shoustin Avatar answered Oct 14 '22 06:10

Maxim Shoustin