My idea is to create a multifunctional controller, ie: The controller will be unique, but it will vary the views (in the form of CRUD).
Follows the files, structure, and error message.
However, when I added a new router inside the .config file, I set it up and put a resolve it conflicts .controller . For when a value is empty it gives error. I created a project which in AngularJs 1.6.9 where I made the structure like this:
Controller
classTypeCtrl{constructor(type,types,TypeService,$state){'ngInject'this._service=TypeService;this._type=type;this._types=types;this._$state=$state;}gettypes(){returnthis._types;}settypes(types){this._types=types;}gettype(){returnthis._type;}settype(value){this._type=value;}view(id){this._$state.go("app.typeView", {id: id} );
}
edit(id){
this._$state.go("app;typeEdit", {id: id});
}
delete(id){
alert("Tem certeza que gostaria de excluir este tipo ?");
}
}
export default TypeCtrl;
Config
function TypeConfig($stateProvider) {
'ngInject';
$stateProvider
.state('app.type', {
url: '/manager/type',
controller: 'TypeCtrl',
controllerAs: '$ctrl',
templateUrl: 'manager/type/index.html',
title: 'Cadastro de Tipo',
resolve : {
types: function(TypeService){
return TypeService.findAll(result => {
return result;
});
},
type: function(TypeService, $state, $stateParams) {
if($stateParams.id) {
return TypeService.findById($stateParams.id).then(result => {
return result;
});
}
}
}
})
.state('app.typeView', {
url: '/manager/type/:id/view',
controller: 'TypeCtrl',
controllerAs: '$ctrl',
templateUrl: 'manager/type/view.html',
title: 'Visualizar Tipo',
resolve: {
type: function(TypeService, $state, $stateParams) {
return TypeService.findById($stateParams.id).then(result => {
return result;
});
}
}
});
};
export default TypeConfig;
Module
import angular from 'angular';
import TypeConfig from './type.config';
import TypeCtrl from './type.controller';
let typeModule = angular.module('app.type', []);
typeModule.config(TypeConfig);
typeModule.controller('TypeCtrl', TypeCtrl);
export default typeModule;