Angular,在运行块中更改变量值

时间:2016-10-21 02:07:36

标签: angularjs

我有一个运行块,代码是

.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,但maskPlaceholder中的templateOptions仍然相同。

如何更新maskPlaceHolder的{​​{1}}?

感谢

1 个答案:

答案 0 :(得分: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('');
});