gpt4 book ai didi

javascript - 修改方法以显示成功/失败消息。 AngularJS

转载 作者:行者123 更新时间:2023-12-02 15:25:20 26 4
gpt4 key购买 nike

我对 angularJS 还很陌生,我似乎找不到一个好方法来显示 Save 方法返回的成功或错误消息。

html 代码如下:

<form role="form">
<div class="panel-body">

<div class="panel-body">
<img src="/assets/doge.jpg" alt="Doge">
</div>

<div class="container">
<div class="input-group">
<span class="input-group-addon" id="tec-nombre">Nombre del
Tecnico:</span><input type="text" class="form-control"
data-ng-model="tecnico.nombre" aria-describedby="tec-nombre">
<div role="alert">
<span class="error"
data-ng-show="myForm.nombreTecnico.$error.required">
Required!</span>
</div>
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-legajo">Legajo del
Tecnico:</span><input type="number" class="form-control"
data-ng-model="tecnico.legajo" aria-describedby="tec-legajo">
<div role="alert">
<span class="error"
data-ng-show="myForm.legajoTecnico.$error.required">
Required!</span>
</div>
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-email">Email del
Tecnico:</span><input type="email" class="form-control"
data-ng-model="tecnico.email" aria-describedby="tec-email">
<div role="alert">
<span class="error"
data-ng-show="myForm.emailTecnico.$error.required">
Required!</span>
</div>
</div>
<div class="input-group">
<span class="input-group-addon" id="tec-interno">Interno del
Tecnico:</span><input type="text" class="form-control"
data-ng-model="tecnico.interno" aria-describedby="tec-interno">
<div role="alert">
<span class="error"
data-ng-show="myForm.nombreTecnico.$error.required">
Required!</span>
</div>
</div>
</div>
</div>
<div class="form-group">
<label class="col-md-2"></label>
<div class="col-md-4">
<a href="#/" class="btn">Cancel</a>
<a data-ng-click="saveTecnico(tecnico);" href="#/test" class="btn btn-primary">Actualizar
{{tecnico.legajo}}</a>
<button data-ng-click="deleteCustomer(customer)"
data-ng-show="customer._id" class="btn btn-warning">Delete</button>
</div>
</div>

这是 Angular 代码:

      angular.module('incidente', [ 'ngRoute' , 'ui.tree' ])

.config([ '$routeProvider', function($routeProvider) {
$routeProvider.when('/', {
templateUrl : 'partials/home.html'
}).when('/incidente/:codename', {
templateUrl : 'partials/incidente.html',
controller : 'IncidenteController'
}).when('/incidentes', {
templateUrl : 'partials/incidentes.html',
controller : 'IncidentesController'
}).when('/tecnicos', {
templateUrl : 'partials/tecnicos.html',
controller : 'TecnicosController'
}).when('/tecnico/:legajo', {
templateUrl : 'partials/tecnico.html',
controller : 'TecnicoController'
}).when('/sistema/:nombre', {
templateUrl : 'partials/sistema.html',
controller : 'SistemaController'
}).when('/sistemas', {
templateUrl : 'partials/sistemas.html',
controller : 'SistemasController'
}).when('/hardware/:codename', {
templateUrl : 'hardware.html',
controller : 'HardwareController'
}).when('/hardwares', {
templateUrl : 'partials/hardwares.html',
controller : 'HardwaresController'
}).when('/software/:codename', {
templateUrl : 'partials/software.html',
controller : 'SoftwareController'
}).when('/softwares', {
templateUrl : 'partials/softwares.html',
controller : 'SoftwaresController'
}).when('/readme', {
templateUrl : 'partials/readme.html',
controller : ''
}).when('/test', {
templateUrl : '/partials/tecnicos.html',
controller : 'TecnicosController'
}).otherwise({
redirectTo : '/'
});
} ])

.controller('home', function($scope, $http) {
$http.get('/resource/').success(function(data) {
$scope.greeting = data;
})
})

.controller(
'navigation',

function($rootScope, $scope, $http, $location) {

var authenticate = function(credentials, callback) {

var headers = credentials ? {
authorization : "Basic "
+ btoa(credentials.username + ":"
+ credentials.password)
} : {};

$http.get('user', {
headers : headers
}).success(function(data) {
if (data.name) {
$rootScope.authenticated = true;
} else {
$rootScope.authenticated = false;
}
callback && callback();
}).error(function() {
$rootScope.authenticated = false;
callback && callback();
});

}

authenticate();
$scope.credentials = {};
$scope.login = function() {
authenticate($scope.credentials, function() {
if ($rootScope.authenticated) {
$location.path("/");
$scope.error = false;
} else {
$location.path("/login");
$scope.error = true;
}
});
};
})

.controller(
'IncidenteController',
[
'$scope',
'$http',
'$routeParams',
function($scope, $http, $routeParams) {

var urlbase = "http://localhost:8080/";

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var code = $routeParams.codename;

console.log(code);

var onIncidenteComplete = function(response) {

try {
$scope.incidente = response.data;
} catch (error) {
console.error(error);
}
};

$http.get(urlbase + "get/incidente/" + code).then(
onIncidenteComplete, onError);

$scope.saveIncidente = function(incidente) {
console.log(incidente);
return $http.post(urlbase + "set/incidente/" + incidente)
};

} ])

.controller(
'IncidentesController',
[
'$scope',
'$http',
function($scope, $http) {

var urlbase = "http://localhost:8080/";

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var onIncidenteComplete = function(response) {

try {
$scope.incidentes = angular
.fromJson(response.data);
console.log($scope.incidentes);
} catch (error) {
console.error(error);
}
};

$http.get(urlbase + "get/incidente/").then(
onIncidenteComplete, onError);

} ])

.controller(
'TecnicoController',
[
'$scope',
'$http',
'$routeParams',
function($scope, $http, $routeParams) {

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var urlbase = "http://localhost:8080/";

var legajo = $routeParams.legajo;

var onTecnicoComplete = function(response) {
try {
$scope.tecnico = response.data;
} catch (error) {
console.error(error);
}
};

$http.get(urlbase + "get/tecnico/" + legajo)
.then(onTecnicoComplete, onError);

$scope.saveTecnico = function(tecnico) {
return $http.post(urlbase + "set/tecnico/", tecnico)
};
This is the function that saves the tecnico and should show the error/success message.
} ])

.controller(
'TecnicosController',
[
'$scope',
'$http',
function($scope, $http) {

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var onTecnicoComplete = function(response) {
$scope.tecnicos = response.data;
};

$http.get("http://localhost:8080/get/tecnico/")
.then(onTecnicoComplete, onError);

} ])

.controller(
'SistemasController',
[
'$scope',
'$http',
function($scope, $http) {

var urlbase = "http://localhost:8080/get/";

var onError = function(reason) {
$scope.error = "No se pudo encontrar";
};

var onSistemaComplete = function(response) {
$scope.sistemas = response.data;
};

$http.get(urlbase + "sistema/").then(
onSistemaComplete, onError);

} ]);

到目前为止只是一个重定向,但我想在重定向之前显示成功或错误消息,以帮助用户了解发生了什么。

最佳答案

你可以使用

    $scope.$on('$routeChangeStart', function(next, current) { 
... you could trigger something here ...
});

并且您应该创建一个在更改路线时显示某些内容的服务。您还有其他事件:

  • $routeChangeSuccess
  • $routeChangeError
  • $routeUpdate

关于javascript - 修改方法以显示成功/失败消息。 AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33739855/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com