2016-12-13 1 views
0

정식 객체 필드에 지침을 추가하려면 어떻게해야합니까?각도 형식을 사용하여 ng-cnpj 지시문을 추가 할 수 없습니다

나는 ng-cnpj (https://github.com/gil/ng-cpf-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 
     } 
    } 
    }); 

}); 

})(); 
관련 문제