如何从指令中的其余调用方法中获取变量值?

How to get variable value out side from the rest call method in Directive?

我正在执行一个指令,在指令中我调用了一个 rest 调用,然后获取一些数据。所以我在变量中存储一个值,我想将变量值放在 return 语句指令中。

我正在分享代码 work.js

angular.module('Smart.Form').directive('changeWrok', function (Rh) {
  Rh.one('/test/work').one('app').one('profiles').get().then(function (response) {
    var setValue = response['value-engine'];      // in setValue(like 4 or 5 or 7)  
  });


  return {
    restrict: 'E',
    // replace: false,
    templateUrl: 'app/plain/directives/change-work.tpl.html',
    link: function (scope, form) {
      form.bootstrapValidator({
        row: {
          valid: 'field-success',
          invalid: 'field-error'
        },
        fields: {
          riskScore: {
            validators: {
              notEmpty: {
                message: 'integer is required'
              },
              greaterThan: {
                value: setValue,
                message: "integer should be positive"
              }
            }
          },
          radioRiskGroup: {
            validators: {
              notEmpty: {
                message: 'Risk Group is required'
              }
            }
          },

        }
      });

     }
    }
  }
});

在 Rh.one setValue 即将到来,但我不能出去。我也试过 $scope.setValue,但是这个指令不起作用。控制台中会出现一些错误。所以我想在 return 语句中设置 setValue 值。

value: setValue, after calling restcall setValueshould set.

我建议您将 REST 调用放在指令控制器中,这样您就可以将 setValue 绑定到本地范围:

return {
  restrict: 'E',
  templateUrl: 'app/plain/directives/change-work.tpl.html',
  controller: function($scope, Rh) {
    Rh.one('/test/work').one('app').one('profiles').get().then(function (response) {
      $scope.setValue = response['value-engine']; 
    })
  },
  link: function(scope, form) {
    ...
              greaterThan: {
                value: scope.setValue, //<-----
                message: "integer should be positive"
              }
    ...
  }
}