我有AngularCtrl,我收到一个参数,如果它为null,它只是不执行函数:
HTML:
<button type="submit" class="btn blue" ng-click="insertar();">Accept</button>
角度控制器
$scope.catalogoPadre = $stateParams.catalogopadre;
if ($scope.catalogoPadre != null) {
cargarCatalogo();
}
function cargarCatalogo() {
apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/", null,
function (res) {
$scope.Catalogos = res.data;
$scope.selected = $scope.Catalogos[0];
inicial
$scope.filtro($scope.selected);
}, errorCatalogo);
正如您所看到的,cargarCatalogo
是一个填充select
问题是当我尝试执行insertar
函数时(即ng-click
视图中),当我发送所有参数时,包括$scope.selected.ID
我进入cargarCatalogo()
函数运行正常,但是如果我想在没有$scope.selected.ID
的情况下执行它,那么在我调试chrome console时它不会达到insertar
函数。
角度控制器第二部分
$scope.filtro = function (selected) {
$scope.selectedID = selected.ID;
$scope.insertar = insertar;
function insertar() {
if ($scope.catalogoid != null) {
var url = "../../api/Catalogo/UpdateCatalogoRegistro/" + $scope.Codigo + "/" + $scope.Nombre + "/" + $scope.catalogoid + "/" + $scope.Catalogo;
if ($scope.selected.ID != null) {
url = url + "/" + $scope.selected.ID;
}
apiService.post(url , null,
function (response) {
bootbox.alert("Operación Exitosa!");
$state.go("root.catalogosgenericos");
},
function (response) {
}
});
}
那里有什么不对?如果我有一个验证
,如果我不发送所有参数,为什么$scope.insertar = insertar;
'不会执行
if ($scope.selected.ID != null) {
url = url + "/" + $scope.selected.ID;
}
此致
更新
我将代码更改为alphapilgrim注释,但当insertar
为空时,它仍然不会触发$scope.selected.ID
操作
代码:
function cargarCatalogo() {
apiService.get("../../api/Catalogo/GetCatalogoPadre/" + $scope.Catalogo + "/", null,
function (res) {
.
$scope.Catalogos = res.data;
$scope.selected = $scope.Catalogos[0];
$scope.filtro($scope.selected);
}, errorCatalogo);
$scope.filtro = function (selected) {
$scope.selectedID = selected.ID;
$scope.insertar = function() {
$scope.selectedID = selected.ID;
if ($scope.catalogoid != null) {
var url = "../../api/Catalogo/UpdateCatalogoRegistro/" + $scope.Codigo + "/" + $scope.Nombre + "/" + $scope.catalogoid + "/" + $scope.Catalogo;
if ($scope.selected.ID != null) {
url = url + "/" + $scope.selected.ID;
}
apiService.post(url, null,
function(response) {
bootbox.alert("Operación Exitosa!");
$state.go("root.catalogosgenericos");
},
function(response) {
if (response.status == "500") {
window.location = "/";
} else {
bootbox.alert(response.status + " " + response.statusText + "<br/>" + response.data.errores);
}
});
}
整个HTML
<div class="page-head">
<div class="page-title">
<h1>
Catálogos Genéricos
<small>Nuevo Registro de: {{tipoCatalogo}}</small>
</h1>
</div>
</div>
<div class="row">
<div class="col-md-12">
<!-- BEGIN Portlet PORTLET-->
<div class="portlet box blue">
<div class="portlet-title">
<div class="caption">
Catálogos
</div>
<div class="tools">
<a href="javascript:;" class="collapse" data-original-title="" title=""> </a>
<!-- <a href="javascript:;" class="reload" data-original-title="" title=""> </a>-->
<a href="javascript:;" class="fullscreen" data-original-title="" title=""> </a>
</div>
</div>
<div class="portlet-body">
<!-- PITE - INICIA TABS -->
<div class="row">
<div class="col-md-12">
<!-- BEGIN Portlet PORTLET-->
<div class="portlet box red">
<div class="portlet-title">
<div class="caption" id="tab_titulo">
Nuevo registro de: {{tipoCatalogo}}
</div>
<div class="tools">
<a href="javascript:;" class="collapse"> </a>
<a href="javascript:;" class="reload" ng-click="fill(conductor.ID); actualizardoctos();"> </a>
<a href="javascript:;" class="fullscreen"> </a>
</div>
</div>
<div class="portlet-body">
<div class="tab-content">
<div class="tab-pane fade active in" id="tab_2_1">
<div class="form-group">
<form id="detalle" name="detalle">
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="form-group">
<label> Código: </label>
<input type="text" class="form-control" id="Codigo" name="Codigo" ng-model="Codigo" maxlength="3" required/>
<span ng-show="detalle.Codigo.$error.maxlength || detalle.Codigo.length" class="required">El Código debe contener 3 caracteres</span>
</div>
</div>
<div class="col-xs-12 col-sm-6 col-md-4">
<div class="form-group">
<label> Nombre: </label>
<input type="text" class="form-control" id="Nombre" name="Nombre" ng-model="Nombre" maxlength="255" required />
</div>
</div>
<div class="col-xs-12 col-sm-4 col-md-4">
<div class="form-group" ng-init="cargarCatalogo()">
<label ng-hide="Catalogos.length==0">Catalogo Padre: </label>
<select class="form-control" ng-hide="Catalogos.length==0" ng-change="filtro(selected)" ng-model="selected" ng-options="item.Nombre for item in Catalogos "></select>
<br />
</div>
</div>
</div>
<hr width="100%" style="color: #2F353B;">
<div class="form-actions">
<div class="row">
<div class="text-right col-md-12">
<button type="submit" class="btn blue" ng-click="insertar();">Aceptar</button>
<button type="button" class="btn default" ng-click="regresar();">Cancelar</button>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div ui-view> </div>
答案 0 :(得分:0)
您应该考虑使用controllerAs语法而不是将方法附加到$ scope。它需要一点点重构,但会产生更清晰的代码。
这是一个plnkr,显示将代码的简化版本转换为controllerAs语法:http://plnkr.co/edit/yZuB5eEiZW15sNzqBIGS?p=preview
评论中提及alphapilgrim,您在insertar
内定义了filtro
方法。如果永远不会调用filtro
方法,这种情况很可能就是这种情况,insertar
将为空。
有关如何切换到controllerAs语法的详细介绍,请参阅https://toddmotto.com/digging-into-angulars-controller-as-syntax/。理智的努力是值得的。
答案 1 :(得分:0)
而不是:
if ($scope.catalogoPadre != null) {
cargarCatalogo();
}
请使用:
if ($scope.catalogoPadre) {
cargarCatalogo();
}
它更常见,可能会解决您的问题,因为它还会检查未定义的内容。
答案 2 :(得分:0)
您可以保持按钮不变,并将insetar
功能更改为范围功能。这应该让你去。这应该立即启动该功能,然后您只需将其调整为缺少的功能。
$scope.insertar = function() {
$scope.selectedID = selected.ID;
if ($scope.catalogoid != null) {
var url = "../../api/Catalogo/UpdateCatalogoRegistro/" + $scope.Codigo + "/" + $scope.Nombre + "/" + $scope.catalogoid + "/" + $scope.Catalogo;
if ($scope.selected.ID != null) {
url = url + "/" + $scope.selected.ID;
}
apiService.post(url, null,
function(response) {
bootbox.alert("Operación Exitosa!");
$state.go("root.catalogosgenericos");
},
function(response) {
}
});
};