【问题标题】:Directive two way binding watch property指令双向绑定监视属性
【发布时间】:2017-05-04 23:42:49
【问题描述】:

我正在尝试在我的 angularjs 应用程序中使用指令,这是我尝试应用的第一个指令,所以我不确定它是否正确。

问题是我想将 ui-select 指令包装到另一个指令中,然后如果选择了新值,我想观察选择。我能够填充选择,但我不知道为什么它不触发手表......这是控制器:

.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 : '@'
        }
    }}]);

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>

最后这就是我调用指令的方式:

<div ng-controller="IngredientesDatosGeneralesController as cntrl">
    <combo-directive 
        seleccionado="cntrl.proveedorSeleccionado" 
        descripcion="formulario.proveedor"
        elementos="cntrl.proveedores">
    </combo-directive>
</div>

提前致谢

【问题讨论】:

  • 能否包含完整的控制器和模板?
  • 感谢您对@mikwat 的关注!我刚刚编辑了文本,问题是当我更改 select 的值时,控制器中的 watch 方法永远不会触发
  • 为了帮助调试,您可以在指令中添加link 函数和监视吗?让我们先验证一下ui-select 确实是先更新模型。
  • 抱歉耽搁了@mikwat,我几天没在家。你是对的,我放了一个链接方法,用手表观察指令的“seleccionado”变量,当我改变选择的值时没有被触发

标签: angularjs angularjs-directive ui-select


【解决方案1】:

我发现发生了什么...我不知道为什么,但是如果我将此配置放入指令中并使用“cntrl”。 HTML 中 "seleccionado" 和 "elementos" 值之前的前缀,它可以正常工作。

'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
        }
}]);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-23
    • 2018-02-11
    • 2015-03-07
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 2016-03-05
    相关资源
    最近更新 更多