Angularjs 指令双向绑定监视属性
我尝试在我的angularjs应用程序中使用指令,这是我第一次尝试应用,所以我不确定它是否正确 问题是,我想将ui select指令包装到另一个指令中,然后如果选择了新值,我想查看selec。我可以填充选择,但我不知道为什么它不会触发手表。。。这是控制器:Angularjs 指令双向绑定监视属性,angularjs,angularjs-directive,ui-select,Angularjs,Angularjs Directive,Ui Select,我尝试在我的angularjs应用程序中使用指令,这是我第一次尝试应用,所以我不确定它是否正确 问题是,我想将ui select指令包装到另一个指令中,然后如果选择了新值,我想查看selec。我可以填充选择,但我不知道为什么它不会触发手表。。。这是控制器: .controller('IngredientesDatosGeneralesController' ,['$scope', 'PrivateAlergenosUtilsService',
.controller('IngredientesDatosGeneralesController' ,['$scope', 'PrivateAlergenosUtilsService',
'PrivateRestauranteService', 'PrivateIngredienteService',
function($scope, PrivateAlergenosUtilsService, PrivateRestauranteService,
PrivateIngredienteService){
var _this = this;
_this.PrivateIngredienteService = PrivateIngredienteService;
_this.proveedorSeleccionado = null;
_this.proveedores = [];
PrivateRestauranteService.getProveedores().then(
function(proveedores){
_this.proveedores = proveedores;
},
function(error){
_this.proveedores = [];
}
);
$scope.$watch('cntrl.proveedorSeleccionado', function(newValue,oldValue){
if (newValue && newValue!=oldValue){
_this.PrivateIngredienteService.getIngregienteDTO().id = _this.proveedorSeleccionado.id;
}
}, true);
}]);
以下是指令:
.directive('comboDirective', [
function(){
return {
restrict : 'E',
templateUrl: 'resources/js/private/views/utils/combo/combo.html',
scope : {
seleccionado : '=',
elementos : '=',
descripcion : '@'
}
}}]);
<div ng-controller="IngredientesDatosGeneralesController as cntrl">
<combo-directive
seleccionado="cntrl.proveedorSeleccionado"
descripcion="formulario.proveedor"
elementos="cntrl.proveedores">
</combo-directive>
</div>
combo.html:
<div class="col-xs">
<label translate>{{descripcion}}</label>
</div>
<div class="col-xs">
<div class="selectStyle">
<ui-select ng-model="seleccionado" theme="bootstrap" register-custom-form-control disable-validation-message="" required>
<ui-select-match placeholder="{{'input.seleccionar' | translate}}">{{$select.selected.descripcion}}</ui-select-match>
<ui-select-choices repeat="elemento in elementos | filter: $select.search">
<div ng-bind-html="elemento.descripcion | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>
<i class="fa fa-chevron-down"></i>
</div>
</div>
{{descripion}}
{{$select.selected.description}
最后,我将该指令称为:
.directive('comboDirective', [
function(){
return {
restrict : 'E',
templateUrl: 'resources/js/private/views/utils/combo/combo.html',
scope : {
seleccionado : '=',
elementos : '=',
descripcion : '@'
}
}}]);
<div ng-controller="IngredientesDatosGeneralesController as cntrl">
<combo-directive
seleccionado="cntrl.proveedorSeleccionado"
descripcion="formulario.proveedor"
elementos="cntrl.proveedores">
</combo-directive>
</div>
提前谢谢我发现发生了什么事。。。我不知道为什么,但如果我将此配置放在指令中,并在HTML中的“selectionado”和“elementos”值之前使用“cntrl.”前缀,它会正常工作
'use strict';
angular.module("private.directives")
.directive('comboDirective', [
function(){
return {
restrict : 'E',
templateUrl: 'resources/js/private/views/utils/combo/combo.html',
scope : {
seleccionado : '=',
elementos : '=',
descripcion : '@'
},
controller : ['$scope', function ($scope) {
}],
controllerAs : 'cntrl',
bindToController: true
}
}]);
您能提供完整的控制器和模板吗?感谢您对@mikwat!!我刚刚编辑了文本,问题是当我更改select的值时,控制器中的watch方法不会触发。为了帮助调试,您可以在指令中添加
链接函数和watch吗?让我们先验证一下ui-select
实际上是在更新模型。很抱歉延迟@mikwat,我有几天不在家。您是对的,我使用了一个link方法,其中一个手表监视指令的“seleccionado”变量,当我更改select的值时,它不会被触发