How to get id to edit given with Angular?

2

I need to get the account id that is displayed in the html below. I need to get this id to take to my backend and bring the data and edit, but I can not.

Here are my codes:

<div class="col-md-6 tabelaEntradas" ng-controller="EditarContaCtrl">
        <table width="400">
            <tr>
                <th>Subcategoria</th>
                <th>Conta</th>
                <th>Editar</th>
            </tr>
            <tr ng-repeat="contaE in contasEntrada">
                <td>{{contaE.subcategoria}}</td>
                <td>{{contaE.conta}}</td>
                <td><a href="#/editarConta/{{contaE.idconta}}">editar</a></td>
            </tr>
        </table>
    </div>

My controller:

app.controller("EditarContaCtrl", function ($scope, $http, $window, $routeParams) {

var idempresa = $window.localStorage.getItem('idemp');
var empresa = $window.localStorage.getItem('empresa');
var usuario = $window.localStorage.getItem('usuario');
var idusuario = $window.localStorage.getItem('idusuario');

$scope.empresa = empresa;
$scope.usuario = usuario;

var pegarContaEntrada = function(contaE){
    console.log(contaE);
}

pegarContaEntrada();

});
    
asked by anonymous 20.01.2017 / 18:08

2 answers

0

Make a button that calls an onClick function. And within this function you send the updated data. Example:

<button ng-click="enviaConta(contaE.conta)">
Envia conta
</button>

And then in your controller you define a function named sendContact.

var enviaConta= function(conta){
   $http.post('/myServerUrl', conta, options).then(successCallback, errorCallback);
};
    
20.01.2017 / 18:13
0

To do what you're looking for: "Click the link redirect to another url and get the parameter passed in the url" you will need to inject the ui-router or ng-router into your application, the example below was mounted using the ui-router.

Do not forget to get the parameter value inside the controller using $ stateParams as I added it to the controller

//O Exemplo que eu passei utiliza o ui-router, geralmente nos projetos angular é usado o ui-router ou ng-router mas a principio os fundamentos são os mesmos

(function () {
    //Adicionando o modulo ui-router na sua aplicação Angular
    'use strict';
    angular.module('app', [
        'ui.router'

    ]);

})();


(function () {
    'use strict';
    angular.module('app')

        .config(configRouter);
    /** @ngInject */
    function configRouter($stateProvider, $urlRouterProvider) {

        $stateProvider

            .state('home', {
                url: "/",
                templateUrl: 'home/home.html',
                controller: 'HomeController',
                controllerAs: 'home',
            })

            /*Na url você coloca os parametros de acordo com a documentação.
             Nesse caso eu forcei o parametro aceitar somente inteiro,
             caso o parametro seja opicional utilizar "?" ex: url: "/editarConta?id",
             */
            .state('editarConta', {
                url: "/editarConta/{id:int}",
                templateUrl: 'editarConta/editarConta.html',
                controller: 'EditarContaCtrl',
                controllerAs: 'conta',
            });


        $urlRouterProvider.otherwise('/');
        $urlRouterProvider.when('', '/home');

    }

})();


(function () {
    'use strict';

    angular
        .module('app')
        .controller('EditarContaCtrl', EditarContaCtrl);

    /** @ngInject */
    function EditarContaCtrl($stateParams) {
    //Os valores dos parametros ficam no $stateParams
    console.log($stateParams.id);

    }
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divclass="col-md-6 tabelaEntradas" ng-controller="EditarContaCtrl">
        <table width="400">
            <tr>
                <th>Subcategoria</th>
                <th>Conta</th>
                <th>Editar</th>
            </tr>
            <tr ng-repeat="contaE in contasEntrada">
                <td>{{contaE.subcategoria}}</td>
                <td>{{contaE.conta}}</td>
                <td><a ui-sref="editarConta({id: contaE.id})">editar</a></td>
            </tr>
        </table>
    </div>
    
22.08.2017 / 14:54