2015-04-30 25 views
0

我試圖僅在選擇另一個組合框時啓用select2字段。這適用於文本框,正常輸入或甚至簡單的select元素,但是當我嘗試在使用Ajax調用的select2中使用ng-disabled時,它不起作用。使用ng-disabled與輸入select2

這裏是我的代碼部分:

<section class="col col-sm-4"> 
    <label class="label" data-localize="Tipo de Participante"></label> 
    <select id="select_tipo_participante" name="select_tipo_participante" ng-model="filtros.F_ID_TIPO_PARTICIPANTE.Valor" 
     class="select2" data-placeholder="Selecione..." 
     ng-disabled="filtros.F_ID_USUARIO.Valor == null"> 
     <option value="null" data-localize="Selecione uma opção"></option> 
     <option ng-repeat="tipo in dados_listas.tipos_participante" value="{{tipo.ID_TIPO_PARTICIPANTE}}"> 
      {{tipo.NM_TIPO_PARTICIPANTE}} 
     </option> 
    </select> 
</section> 

<section class="col col-sm-4"> 
    <label class="label" data-localize="Novo Usuário"></label> 
    <input name="select_novo_usuario" id="select_novo_usuario" ng-model="filtros.F_ID_USUARIO.Valor" ng-disabled="filtros.F_ID_TIPO_PARTICIPANTE.Valor == null"/> 
</section> 
<section class="col col-sm-12"> 
    <label class="label" data-localize="Justificativa"></label> 
    <textarea rows="3" class="form-control" name="justificativa_redesignacao" ng-model="DS_JUSTIFICATIVA" ng-disabled="filtros.F_ID_TIPO_PARTICIPANTE.Valor == null"></textarea> 

我想,直到前一個選擇,以保持第二個元素(ID =「select_novo_usuario」)禁用。

使用ng-disabled的textarea工作得很好。

這裏是我的Ajax調用:

$("#select_novo_usuario").select2({ 
    minimumInputLength: 4, 
    allowClear: true, 
    placeholder: localize.localizeText('tooltip_select_interessado'), 
    ajax: { 
     url: BACKEND_URL + 'usuario_interessado', 
     dataType: 'json', 
     type: "GET", 
     quietMillis: 50, 
     data: function (term) { 
       var filter = 'NM_USUARIO,like,' + term; 
      return { 
       filters: filter, 
       filter_id_interessado: 'ID_USUARIO,notin,teste.teste' 
      }; 
     }, 
     results: function (data) { 
      return { 
       results: $.map(data, function (item) { 
        return { 
         text: item.NM_USUARIO, 
         slug: item.NM_USUARIO, 
         id: item.ID_USUARIO 
        } 
       }) 
      }; 
     }, 
    }, 
}); 

$("#select_novo_usuario").on("select2-selecting", function(e) { 
    $timeout(function(){ 
     $scope.$evalAsync(function() { 
      $scope.filtros.F_ID_USUARIO.Valor = e.val; 
     }); 
    }); 
}); 

我怎樣才能得到它的工作?

+0

試NG-禁用= – tpie

+0

它沒有,如果你CONSOLE.LOG($ scope.filtros.F_ID_TIPO_PARTICIPANTE工作@tpie – lucianthomaz

+0

。 Valor), 你會得到什麼?觸摸選擇之前。 – tpie

回答

0

我無法使用ng-disabled使它工作,所以我在我的.js文件中做了它,使選擇我想禁用響應前一個的$ watch。 這裏是我的代碼作爲SAMPE: 「filtros.F_ID_TIPO_PARTICIPANTE.Valor」

$scope.$watch('filtros.F_ID_TIPO_PARTICIPANTE.Valor', function(newVal, oldVal, scope) { 
if(newVal != null) { 
    $('#select_novo_usuario').select2("enable", true); 
    $('#select_novo_usuario').select2("val", ""); 
} 
$('#select_novo_usuario').select2("enable", false); 
$('#select_novo_usuario').select2("val", "");}); 
0

看來你已經設置正確。

嘗試刪除ng-disabled="filtros.F_ID_USUARIO.Valor == null上的選擇 他們禁用彼此,它出現。

如果我正確地讀取了您的代碼,那麼輸入內容纔會被填充,直到選擇了某些內容爲止,對不對?由於它們似乎是相互禁用的,似乎都不應該啓用。

如果select不變,它應該設置爲null。輸入和textarea上的ng-disabled值都應該返回null。這裏是一個plunker表明:

Plunker

輸入和textarea的是禁用,直到你在選擇中選擇一個項目。

+0

在我的設置中,ng-disabled似乎不適用於select2,即使它可以很好地與其他選擇元素或任何其他元素一起使用。我發現它的工作方式是將其設置在我的.js文件中。我已經發布了我的樣本作爲答案。感謝您的幫助。 – lucianthomaz