Get value from directive to $scope in controller

user500468

In my Directive, I've getting some data from my backend like this:

var monster_info = angular.element(this).find("img").attr("title");
                    $http.get("lib/terrain.php", {params: { monster_data:monster_info}}).success(function(data) {
                            console.log(data);
                    });

I want this data to appear at my site, so I've tried to declare a variable like this in my controller: $scope.fish = [];

Then I just assign the data value like this in my directive: $scope.fish = data;

But nothing is printed out. How does the communication between controller and directive work? How should I share this kind of data?

Here is my whole directive:

angular.module('gameApp_directives').
  directive('mapActivity', function($http) {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            scope.$watch('tabledata', function() {

             angular.element('.click#1').addClass('dotted').html($("<img src='images/dot.png'>"));          
                var j = null;
                for(var i = 1; i <= 4; i++)
                {
                    $.ajax({
                        type: 'GET',
                        url: 'lib/terrain.php',
                        dataType: 'html',
                        data: {i: i},
                        success: function(data) {
                            var randomRuta = Math.floor((Math.random() * 100) + 1);
                            angular.element('.click#'+randomRuta).addClass('monster').html($("<img src='images/blackdot.png' title='"+data+"'>"));                  
                        },
                        error: function(xhr, ajaxOptions, thrownError) { alert(thrownError); }
                    });
                    j=i;
                }  
                angular.element('.click').click(function() {
                    if(angular.element(this).hasClass('monster'))
                    {
                        var monster_info = angular.element(this).find("img").attr("title");
                        $http.get("lib/terrain.php", {params: { monster_data:monster_info}}).success(function(data) {
                                console.log(data);
                        });



                        /*if(confirm('Vill du anfalla monster?'))
                        {
                            alert("Du vann");
                            angular.element('.click.monster'+j).empty();
                            angular.element('.click.monster').removeClass('monster'+j);

                            angular.element('.click.dotted').empty();
                            angular.element('.click.dotted').removeClass('dotted');
                            angular.element(this).addClass('dotted');
                            angular.element('.click.dotted').html($('<img src="images/dot.png">'));
                        }*/
                    }
                    else
                    {
                        angular.element('.click.dotted').empty();
                        angular.element('.click.dotted').removeClass('dotted');

                        if(!angular.element(this).hasClass('dotted'))
                        {
                            angular.element(this).addClass('dotted');
                            angular.element(this).html($('<img src="images/dot.png">'));
                        }
                    }
                });
            });                     
        }
    };
});

Here is my controller:

angular.module('gameApp_controllers')
    .controller('gameCtrl', ['$scope', '$http', '$location', '$sce', '$rootScope', 'link', function($scope, $http, $location, $sce, $rootScope, link) {

        $scope.resultLoaded = false;
        $scope.getMonsters = "1";
        var tabledata = ""; //Variable to store pattern for html table

        $http.post("lib/action.php", {monsters: $scope.getMonsters}).success(function(data) {   
            //tabledata = data; //Assign the pattern
            $scope.result = makeTableFrom(data); //Call the function to build the table based on the pattern
            $scope.resultLoaded = true;
        }).error(function(data) { console.log("error"); });

        $scope.fish = [];
        $scope.safeHtml = function() {
            return $sce.trustAsHtml($scope.result);
        };


        if(link.user) {
            /*$scope.message = "fisk";
            console.log(link.user);*/
        } else {
        /*$scope.message = "Ledsen fisk";
        console.log("Är inte satt");*/
        }
}]).controller('firstPageCtrl', ['$scope','$http','$location','$sce','$rootScope','link', function($scope, $http, $location, $sce, $rootScope, link) {
        $scope.doLogin = function() {
        $http.post("lib/action.php", {username: $scope.username, password: $scope.password}).success(function(data) {
            if(data) {
                link.user = data;
                console.log(link.user);
                $location.path("/game");
            }
        }).error(function(data) {
            console.log(data);
        });
    };
}]);

Anyone who can help me?

Here is my HTML

<div id="layout_game">
<div ng-controller="gameCtrl">
<div ng-if='resultLoaded' id="rightcontent_game">
    <table ng-bind-html="safeHtml()" map-Activity>
    </table>
</div>

<div id="leftcontent_game">
    <div id="leftcontent_top">
        <ul>
            <li><a href="#">Vildmarken</a></li> <li> | </li> <li><a href="#">Marknaden</a></li> <li> | </li> <li><a href="#">Värdshuset</a></li>
        </ul>
    </div>
    {{fish}}
</div>
</div>
</div>
lulu88

I would just broadcast an event from the directive like this:

  $rootScope.$broadcast('fishChange', fishValue);

And then in the controller listen for that event:

  $scope.$on('fishChange', function(event, fishValue) {

   // Do whatever you want here

});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Access controller scope from directive

From Dev

Change Controller variable value from change in isolate scope directive

From Dev

Passing data from directive to parent scope controller

From Dev

AngularJS directive controller's scope accessible from outside directive, why?

From Dev

Unable to get value from scope using directive

From Dev

Accessing directive scope from a separate controller

From Dev

Update controller scope from directive

From Dev

Get value from Directive into Controller

From Dev

How to update the `$scope` object from `directive` controller

From Dev

Not able to get the value from the `directive` scope

From Dev

Changing a scope value from a directive

From Dev

change controller $scope from a directive

From Dev

Change controller scope value from directive AngularJS

From Dev

Pass value from controller to directive

From Dev

how to get access to controller scope when testing directive with isolate scope

From Dev

Passing variable from controller scope to directive

From Dev

Passing controller scope from directive to partial

From Dev

Pass scope variable from directive to it's controller

From Dev

Angular moving scope variable from controller to directive

From Dev

How to pass scope value to the directive controller

From Dev

Pass a value from a controller to directive

From Dev

Not able to get the value from the `directive` scope

From Dev

How to get value from controller to directive

From Dev

AngularJS $watch controller variable from a directive with scope

From Dev

How to get selected value from ng-autocomplete directive to controller

From Dev

Watch on controller scope from directive link function

From Dev

Change directive value from controller

From Dev

angularjs directive + apply scope value in many controller

From Dev

angularjs controller access directive scope value

Related Related

  1. 1

    Access controller scope from directive

  2. 2

    Change Controller variable value from change in isolate scope directive

  3. 3

    Passing data from directive to parent scope controller

  4. 4

    AngularJS directive controller's scope accessible from outside directive, why?

  5. 5

    Unable to get value from scope using directive

  6. 6

    Accessing directive scope from a separate controller

  7. 7

    Update controller scope from directive

  8. 8

    Get value from Directive into Controller

  9. 9

    How to update the `$scope` object from `directive` controller

  10. 10

    Not able to get the value from the `directive` scope

  11. 11

    Changing a scope value from a directive

  12. 12

    change controller $scope from a directive

  13. 13

    Change controller scope value from directive AngularJS

  14. 14

    Pass value from controller to directive

  15. 15

    how to get access to controller scope when testing directive with isolate scope

  16. 16

    Passing variable from controller scope to directive

  17. 17

    Passing controller scope from directive to partial

  18. 18

    Pass scope variable from directive to it's controller

  19. 19

    Angular moving scope variable from controller to directive

  20. 20

    How to pass scope value to the directive controller

  21. 21

    Pass a value from a controller to directive

  22. 22

    Not able to get the value from the `directive` scope

  23. 23

    How to get value from controller to directive

  24. 24

    AngularJS $watch controller variable from a directive with scope

  25. 25

    How to get selected value from ng-autocomplete directive to controller

  26. 26

    Watch on controller scope from directive link function

  27. 27

    Change directive value from controller

  28. 28

    angularjs directive + apply scope value in many controller

  29. 29

    angularjs controller access directive scope value

HotTag

Archive