1
Using the routes with Angularjs, When entering the Author.html page with the route /Autor
, is listed all authors registered in a table where a link is created for each author’s edition, according to this line of code <a ng-click="setAutor(autor)" ng-href="#!/Autor/Editar/{{autor.ID}}">
, when clicking an author’s edit the route is given /Autor/Editar/:id
, which will open the html page Editar.html
, both pages use the same controller autorController
, only when it opens my page Editar.html
the model object autor.Nome
does not come filled in, the controller from one page to another is "reset" your $Scope?
How do I pass this object from one page to another using the same controller?
App.js
(function () {
var app = angular.module("app", ["ngRoute"]);
app.config(function ($routeProvider) {
$routeProvider
.when("/", { controller: "homeController", templateUrl: "/AngularJS/view/Home/Home.html" })
.when("/Livro", { controller: "livroController", templateUrl: "/AngularJS/view/Livro/Livro.html" })
.when("/Autor", { controller: "autorController", templateUrl: "/AngularJS/view/Autor/Autor.html" })
.when("/Autor/Editar/:id", { controller: "autorController", templateUrl: "/AngularJS/view/Autor/Editar.html" })
.when("/Editora", { controller: "editoraController", templateUrl: "/AngularJS/view/Editora/Editora.html" })
.otherwise({ redirectTo: "/" });
});
}());
autorController.js
(function () {
angular.module("app").controller("autorController", function ($scope, $http) {
$scope.getAll = function() {
$http.get("/api/AutorAPI")
.then(function(data) {
$scope.autores = data.data;
});
}
$scope.setAutor = function(autor) {
$scope.autor = autor;
}
});
}());
Author.html
<div ng-app="app" ng-controller="autorController">
<table>
<thead>
<tr>
<th> </th>
<th> ID </th>
<th> Nome </th>
</tr>
</thead>
<tbody ng-repeat="autor in autores">
<tr>
<th>
<a ng-click="setAutor(autor)" ng-href="#!/Autor/Editar/{{autor.ID}}">
</th>
<th> {{autor.ID}} </th>
<th> {{autor.Nome}} </th>
</tr>
</tbody>
</table>
</div>
Edit.html
<div ng-app="app" ng-controller="autorController">
<label class="label-control"> Nome: </label>
<input type="text" class="form-control" ng-Model="autor.Nome"/>
</div>
Friend, its variables $author and $authors are incorrect. Declares a variable in Angularjs using
$scope
from the beginning. Try using $Scope.author and $Scope.autores. If possible make a Jsfiddle or Plunker example. Obviously your API call will not work, you can exchange the address for this site here that hosts a json for free: http://myjson.com/– Jackson
@Jackson, I corrected where I said I was wrong, I tested so with Scope too, but it didn’t work, I ended up performing several tests and when I reented the code to post here I forgot to put $Scope. Still my problem persists.
– Nicola Bogar