How to Create an Edit View in AngularJs

1

How could I do that by clicking on the Edit button, I could open a form where I filled out all the data to edit the company that appears in the table ??. In the query I only show 6 Basic Data but in reality they are more and I would like to show them in a window and edit them.

I appreciate collaboration, I am very new in this of angularJs

    
asked by Cristian 26.07.2016 в 16:00
source

2 answers

1

Good morning. I'm assuming you're using routing and programming a SPA, so I'll build on this to give you an answer.

In your edit button you must indicate that you change your view, for example:

<button ng-click="edit(company)">Editar</button>

In your controller you define the edit function:

$scope.edit = (company) => {
  $location.path('editar/${company._id}');
};

This will change your path to something like this: /editar/n4503nc . You are supposed to have that URL mapped in the router:

$routeProvider
  ...
  .when('/empresas/editar/:id', {
      template: 'editar-empresa.html',
      controller: 'EditCompanyCtrl'
  });

The EditCompanyCtrl will receive the id parameter that contains the company ID to edit. In the controller what you should do is make a request to the backend via AJAX and obtain the company data with that ID. Once obtained, you only add it to $scope .

app.controller('EditCompanyCtrl', ($scope, $routeParams) => {
  $.get('/api/companies/${routeParams.id}')
    .then((company) => {
      $scope.company = company;
    });
});

For a modal, there is no greater mystery. Just have the modal under the command of the same controller.

    
answered by 26.07.2016 / 17:10
source
0

If you want to do it with a modal, I imagine that you add with a modal too ... because the idea is to reuse the ng-model. for example:

<div class"modal fade" id="modal"> <form>... <buttom type="submit" ng-show="create"></buttom> Crear</div> <buttom type="submit" ng-show="!create">Actualizar</buttom> </form></div>

in the controller you have of course to define a variable "create" that is set to true or false depending on whether you are creating or updating. for example:

$('form').submit(function (e) { e.preventDefault(); if (!$scope.create){ actualizar(); }else{ crear(); } }

    
answered by 26.07.2016 в 20:12