How to pass data from controller to directive from ajax call AngularJS

Vladimir Djukic

I need to send data to directive when call is successful... Here is my ajax call from my controller:

$scope.items ={
                avatar: ""
 };
$scope.addComment = function(segment) {
    commentFactory.saveComment($scope.form.comment,segment,0,0)
    .success(function(data){
        $scope.items.avatar = data.avatar;
    })
    .error(function(data){
       console.log(data); 
    });

    // Reset the form once values have been consumed.
    $scope.form.comment = "";
};

And here is 2 directive first use to submit form and ajax req, second use to update content on client side. I need in second directive to load content form ajax... Problem now is directive not wait for ajax to finish call...

.directive("addcomment", function(){
    return {
        restrict: "E",
        template: '<input type="submit" addcomments class="btn btn-default pull-right" value="Send" />'
    };
})

.directive("addcomments", function($compile){
    return {
        link: function (scope, element, attrs) {
            var html = '<div>'+scope.items.avatar+'</div>';

            element.bind("click", function(){
                angular.element(document.getElementById('space-for-new-comment'))
                            .append($compile(html)(scope));
            })  
        }
    };
});

Any solution for this?

Petr Averyanov

I just want to show you another way of writing this: You want to put some comments, ok in html:

<div class="smartdivforcomments">
    <div ng-repeat="comment in newComments">
        {{comment.avatar}}
    </div>
</div>

In controller: $scope.newComments = []; Function for adding comments:

commentFactory.saveComment($scope.form.comment,segment,0,0)
.success(function(data){
    $scope.newComments.push({avatar : data.avatar});
})
.error(function(data){
   console.log(data); 
});

Answer to your comment to previous question: You bind to click event that is not angular, so you need to use scope.apply to correctly update your view.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angularjs how to pass data from a controller to a directive

From Dev

Angularjs directive to get data from ajax call in controller

From Dev

How Do I call and pass data to a Directive from a Controller Angular

From Dev

Pass ajax data from Controller to directive

From Dev

AngularJS How to call directive function from controller

From Dev

How to pass async data from directive to controller?

From Dev

How to Pass data from directive template to controller?

From Dev

How to pass data from directive to controller?

From Dev

Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

From Dev

angularjs: What is the preferred way to pass data from controller to directive(s)?

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to pass argument to method defined in controller but called from directive in Angularjs?

From Dev

How to pass object from controller to directive without using $scope in angularJS

From Dev

How to pass a function handler from controller into directive isolated scope in AngularJs?

From Dev

How to pass a value from an AngularJS directive to a function in parent controller

From Dev

AngularJS: Pass object from directive to controller

From Dev

Angularjs: Pass calendar value from directive to controller

From Dev

AngularJS Trigger directive from controller call

From Dev

how to pass data from controller to directive using $emit

From Dev

How to call a directive from an another directive in AngularJS?

From Dev

How to pass data from directive to html template - AngularJS

From Dev

AngularJS : How to pass data from directive to controllers function

From Dev

How to pass data from directive to html template - AngularJS

From Dev

how to pass object data from one controller to another controller in angularjs

From Dev

AngularJS: access directive data from controller

From Dev

JS - AngularJS - Get data of an directive from a controller

From Dev

AngularJS Directive not reading data from controller

From Dev

JS - AngularJS - Get data of an directive from a controller

From Dev

AngularJS: How to pass data from view to controller in angularjs

Related Related

  1. 1

    Angularjs how to pass data from a controller to a directive

  2. 2

    Angularjs directive to get data from ajax call in controller

  3. 3

    How Do I call and pass data to a Directive from a Controller Angular

  4. 4

    Pass ajax data from Controller to directive

  5. 5

    AngularJS How to call directive function from controller

  6. 6

    How to pass async data from directive to controller?

  7. 7

    How to Pass data from directive template to controller?

  8. 8

    How to pass data from directive to controller?

  9. 9

    Angularjs pass function from Controller to Directive (or call controller function from directive) - with parameters

  10. 10

    angularjs: What is the preferred way to pass data from controller to directive(s)?

  11. 11

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  12. 12

    How to pass argument to method defined in controller but called from directive in Angularjs?

  13. 13

    How to pass object from controller to directive without using $scope in angularJS

  14. 14

    How to pass a function handler from controller into directive isolated scope in AngularJs?

  15. 15

    How to pass a value from an AngularJS directive to a function in parent controller

  16. 16

    AngularJS: Pass object from directive to controller

  17. 17

    Angularjs: Pass calendar value from directive to controller

  18. 18

    AngularJS Trigger directive from controller call

  19. 19

    how to pass data from controller to directive using $emit

  20. 20

    How to call a directive from an another directive in AngularJS?

  21. 21

    How to pass data from directive to html template - AngularJS

  22. 22

    AngularJS : How to pass data from directive to controllers function

  23. 23

    How to pass data from directive to html template - AngularJS

  24. 24

    how to pass object data from one controller to another controller in angularjs

  25. 25

    AngularJS: access directive data from controller

  26. 26

    JS - AngularJS - Get data of an directive from a controller

  27. 27

    AngularJS Directive not reading data from controller

  28. 28

    JS - AngularJS - Get data of an directive from a controller

  29. 29

    AngularJS: How to pass data from view to controller in angularjs

HotTag

Archive