2016-12-13 26 views
0

任何人都知道如何添加一個指令在我的formly對象字段?我不能添加ng-cnpj指令使用角度形式

我有ng-cnpj(​​)指令,並希望將其添加到我的控制器上的cpf字段中。

.... 
    { 
     key: 'cnpj', 
     type: 'maskedInput', 
     templateOptions: { 
     type: 'text', 
     label: 'CNPJ', 
     placeholder: '00.000.000/0000-00', 
     mask: '99.999.999/9999-99', 
     required: true 
     } 
.... 

回答

0

我解決了它在formlyConfig中創建自定義類型的問題。以下,我的代碼解決方案:

(function() { 
'use strict'; 

angular.module('app.runs', ['formly', 'formlyBootstrap', 'ngMask']) 

.run(function (formlyConfig, $rootScope) { 

    // mask from ngMask 
    formlyConfig.setType({ 
    name: 'maskedInput', 
    extends: 'input', 
    defaultOptions: { 
     ngModelAttrs: { // this is part of the magic... It's a little complex, but super powerful 
     mask: { // the key "ngMask" must match templateOptions.ngMask 
      attribute: 'mask' // this the name of the attribute to be applied to the ng-model in the template 
     }, 
     // applies the 'clean' attribute with the value of "true" 
     'true': { 
      value: 'clean' 
     } 
     }, 
     // this is how you hook into formly's messages API 
     // however angular-formly doesn't ship with ng-messages. 
     // You have to display these messages yourself. 
     validation: { 
     messages: { 
      mask: 'Dado inválido!' 
     } 
     } 
    } 
    }); 


    // My CNPJ Custom Type 
    formlyConfig.setType({ 
    name: 'rbCnpj', 
    extends: 'maskedInput', 
    wrapper: ['bootstrapLabel', 'bootstrapHasError'], 
    template: '<div class="form-group">' + 
       '<input ng-model="model[options.key]" class="form-control" ng-cnpj />' + 
       '</div>', 
    defaultOptions: { 
     templateOptions: { 
     type: 'text', 
     label: 'CNPJ', 
     placeholder: '00.000.000/0000-00', 
     mask: '99.999.999/9999-99', 
     required: true 
     } 
    } 
    }); 

}); 

})();