How do I pass one parameter to another scope with angularjs?

12

I have a screen that contains a list of information, when clicking on some item I can get its number, for example: Item 5 of array .

The problem is that on another screen I need to display the information of the item that was clicked, and I'm not sure how to pass that reference to another scope ( $scope ).

Can anyone help me please?

    
asked by anonymous 01.03.2015 / 00:00

2 answers

9

The recommended way to share values between Controllers is using Services . Following example:

angular.module('minhaApp', [])
    .service('PropriedadesCompartilhadas', function () {
        var ItemSelecionado = '';

        return {
            getItem: function () {
                return ItemSelecionado;
            },
            setItem: function(value) {
                ItemSelecionado = value;
            }
        };
    });

In your controller, you can consume this service via injection:

function Ctrl($scope, PropriedadesCompartilhadas) {
    //Obtém valor
    $scope.ValorSelecionado = PropriedadesCompartilhadas.getItem();

    //Seta valor
    PropriedadesCompartilhadas.setItem(1);
}

Original reference: 'How can I pass variables between controllers?' , original OS.

    
02.03.2015 / 21:24
3

You can configure your route as follows:

 $stateProvider.state('detalhes', {
            url: '/detalhes/{idItem}',
            templateUrl: 'detalhes.html',
            controller: 'DetalhesCtrl'
        });

To pass the id of the item you put in the link:

<a ui-sref="detalhes({idItem: idDoItem})">Nome do item</a>

To get the id of the item sent to the controller would be:

app.controller('DetalhesCtrl', function ($scope, $stateParams) {

    var idItem = $stateParams.idItem; 

});

Then you can use this Id to get the item information. From what I understand of the question I believe that it solves

    
16.02.2016 / 18:14