2016-10-21 50 views
0

我有一个运行块,代码角,改变运行块变量值

.run(['$rootScope', 'formlyConfig', 'appApiCheck', 'licenseExpDateValue', 
    ($rootScope, formlyConfig, appApiCheck, licenseExpDateValue) => { 

    $rootScope.maskPlaceHolder = ''; 

    formlyConfig.setType({ 
     name: 'maskedInput', 
     extends: 'input', 
     template: '<input class="form-control" ng-model="model[options.key]" ng-required="{{licenseExpDateValue.required}}" ng-class="{\'has-error\':licenseExpDateValue.showError}"/>', 
     defaultOptions: { 
      ngModelAttrs: { 
       mask: { 
        attribute: 'ui-mask' 
       }, 
       maskPlaceholder: { 
        attribute: 'ui-mask-placeholder' 
       } 
      }, 
     templateOptions: { 
      maskPlaceholder: $rootScope.maskPlaceholder 
     } 
     } 
    }) 
}]) 

和后,我会从服务器获取数据,并且将改变$rootScope.maskPlaceholder,但maskPlaceholdertemplateOptions仍是相同。

如何更新maskPlaceHoldertemplateOptions

谢谢

+0

您可以尝试使用'angular.copy'而不是直接分配来保留引用,但是我觉得可能有更好的方法。什么是'someConfigData'?它在哪里定义以及它存在的范围? – Phil

+0

@phil,谢谢。我已经更新了我的问题。我正尝试使用这个插件http://angular-formly.com/#/example/integrations/ui-mask –

回答

0

我会这样做与一次性观察员。类似于

.service('UpdateConfig', [ '$rootScope', 'formlyConfig', function ($rootScope, formlyConfig) { 
    var x = { 
    updateConfig: function (maskPlaceHolder) { 
     formlyConfig.setType({ 
     name: 'maskedInput', 
     extends: 'input', 
     template: '<input class="form-control" ng-model="model[options.key]" ng-required="{{licenseExpDateValue.required}}" ng-class="{\'has-error\':licenseExpDateValue.showError}"/>', 
     defaultOptions: { 
      ngModelAttrs: { 
      mask: { 
       attribute: 'ui-mask' 
      }, 
      maskPlaceholder: { 
       attribute: 'ui-mask-placeholder' 
      } 
      }, 
      templateOptions: { 
      maskPlaceholder: maskPlaceholder 
      } 
     }}); 
    }, 
    watcher: $rootScope.$watch('maskPlaceHolder', function (value) { 
     x.updateConfig(value); 
     x.watcher(); 
    }); 
    }; 
    return x; 
}]) 
.run(['$rootScope', 'UpdateConfig', 'appApiCheck', 'licenseExpDateValue', 
    ($rootScope, UpdateConfig, appApiCheck, licenseExpDateValue) => {  
    UpdateConfig.update(''); 
}); 
+0

,这真的很酷。谢谢 –