How to redeem id from one controller to another controller in Angularjs?

Asked

Viewed 652 times

1

I have a mobile app, I have an area where I list all the news that ah, and I have an option to like the news what is happening to me and that I am not able to get the news id to send to mysql I would like to know how I can get the controller id that I list the news and use the news id to use in the controller that tastes the news.

Controller Lista Noticias

.controller('ListaNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
     $http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
        $scope.noticias_home = data;

        $partilharRecursos.set('idNoticia', data.id);
    });
})

Controller Like

.controller('LikeNoticiasHome', function($scope, $http, $stateParams, sessionService, $partilharRecursos) {
    var hasLiked = false;
    $scope.like= function (){

        if (!hasLiked) {
            hasLiked = true;
            $scope.liked = 'Não Gosto';
            $scope.likeCount += 1;

            $http.get("https://www.sabeonde.pt/api/api_like_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });

        } else {
            hasLiked = false;
            $scope.liked = 'Gosto';
            $scope.likeCount -= 1;

            $http.get("https://www.sabeonde.pt/api/api_unlike_noticias.php?post_id="+$partilharRecursos.get("idNoticia")+ "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });
        }     
    }
})

HTML

<div class="row" ng-repeat="noticias in noticias_home">
        <div class="col">
            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}">
                <div style="background: url(https://www.sabeonde.pt/gtm/anexos/posts_home/{{noticias.id_anexo}}.{{noticias.tipo}}); border-top-left-radius:10px; border-top-right-radius:10px; height: 200px; background-size: 100% 100%; background-repeat: no-repeat;">
                </div>
            </a>
            <div style="border-bottom-left-radius:10px; border-bottom-right-radius:10px; height: 100px; background-color: white;">
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td colspan="2" valign="top">
                            <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div style="font-size: 15px; color:black; margin:5px 0px 15px 10px;  font-weight: bold; ">{{noticias.titulo}}</div></a>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                           <div ng-init="liked='Gosto'" ng-click="like()" ng-controller="ListaNoticiasHome" style="margin-left:10px;" class="botao_gosto"><i class="fa fa-heart"></i> {{liked}}</div>
                           <div id="mostra_gostos" class="mostra_gostos">{{noticias.likes}}</div>
                           <a href=""><div class="botao_posts"><i class="fa fa-share-alt"></i> Partilhar</div></a>
                           <a href="#/app/ver-noticia/{{noticias.url_artigo}}/{{noticias.id}}"><div class="botao_posts"><i class="fa fa-search"></i> Ver +</div></a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
    </div>

1 answer

1


Would that be?

EDIT 1

var app = angular.module("App", []);

app.controller("AppController1", function($scope) {
  $scope.varController1 = "JEC nao vai cair!!!";
  $scope.consultarOutroController = function() {
    var scopeOutroController = angular.element('[ng-controller=AppController2]').scope();
    alert(scopeOutroController.varController2);
  };
});

app.controller("AppController2", function($scope) {
  $scope.varController2 = "JEC nasceu campeão!!!";
  $scope.consultarOutroController = function() {
    var scopeOutroController = angular.element('[ng-controller=AppController1]').scope();
    alert(scopeOutroController.varController1);
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="App">
  <div ng-controller="AppController1">
    <input type="button" value="Consultar outro Controlador 1" ng-click="consultarOutroController();" />
  </div>

  <div ng-controller="AppController2">
    <input type="button" value="Consultar outro Controlador 2" ng-click="consultarOutroController();" />
  </div>
</div>

EDIT 2

Another way to share data between controllers is by using $rootScope. This guy works similarly to a global variable in js.

app.controller("AppController1", function($scope, $rootScope) {
    $rootScope.varController1 = "JEC nasceu campeão!!!";
});

app.controller("AppController2", function($scope, $rootScope) {
    alert($rootScope.varController1);
});

EDIT 3

Controller Lista Noticias

.controller('ListaNoticiasHome', function($scope, $rootScope, $http, $stateParams, $partilharRecursos, sessionService) {
    $http.get("https://www.sabeonde.pt/api/api_noticias_home.php").success(function (data) {
        $scope.noticias_home = data;
    });
    $partilharRecursos.set('idNoticia', '<passe aqui o id>');
});

Controller Like

.controller('LikeNoticiasHome', function($scope, $http, $rootScope, $stateParams, $partilharRecursos, sessionService) {
    var hasLiked = false;
    $scope.like= function (){

        if (!hasLiked) {
            hasLiked = true;
            $scope.liked = 'Não Gosto';
            $scope.likeCount += 1;

            $http.get("https://www.sabeonde.pt/api/api_like_noticias.php?post_id="+ $partilharRecursos.get("idNoticia") + "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });

        } else {
            hasLiked = false;
            $scope.liked = 'Gosto';
            $scope.likeCount -= 1;

            $http.get("https://www.sabeonde.pt/api/api_unlike_noticias.php?post_id="+ $partilharRecursos.get("idNoticia") + "&user_id=" + sessionService.get('user_id')).success(function (data) {
                $scope.like_noticias_home = data;
            });
        }     
    }
});

Service that shares resources

.service('$partilharRecursos', function () {
    var property = {};

    return {
        get: function (key) {
            console.log("Key: " + key);
            console.log(property);
            return property[key];
        },
        set: function(key, value) {
            console.log("Key: " + key);
            console.log("Value: " + value);
            property[key] = value;
            console.log(property);
        }
    };
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.