在 AngularJS 中将模型传递给指令

passing a model into a directive in AngularJS

我在一个页面上有 4 个 select 下拉菜单。在每个下面,我希望动态 HTML 使用 angular-formly 基于 selection 生成。

到目前为止我有这个:

ruleSelect.js.erb

angular.module('productsApp')
  .directive('ruleSelect', [
  function() {
    return {
      restrict: 'E',
      replace:  false,
      // require: 'ngModel',
      scope: {
        options: '=',
        ruleBldr: '='
      },
      templateUrl: "<%= asset_path('shared/templates/ruleSelect.html') %>",
      link: function(scope, element, attrs){
      }
    };
 }]);

ruleSelect.html.slim

select.form-control ng-model="value"
  option ng-repeat="field in options | fieldFilter:['templateOptions', 'label']" value="{{$index+1}}"
    | {{field.templateOptions.label}}

form
  formly-form fields="ruleBldr.form[value].fieldGroup"

但是在作用域上设置了 optionsruleBldr 是空的。

HTML 页数

.row
  .col-md-offset-6.col-md-6 ng-repeat="obj in ruleB.formObjects"
    label.control-label () {{obj.fieldGroup[0].template}}
    rule-select options='obj.fieldGroup' ruleBldr="obj"

value in ruleB.form[value] 未在任何地方定义。

如果要根据在 <select> 中选择的值创建表单,您需要将 ng-model 添加到 <select>。请注意 ng-model 应该始终是一个对象。

link: function(scope, element, attrs){
   scope.selectModel ={}
}

select.form-control ng-model="selectModel.value"

formly-form fields="ruleBldr.form[selectModel.value].fieldGroup"

由于问题描述很差,所以其中有些是猜测。我也不知道你的模板系统

的正确 html 语法

我也不确定你是否需要从控制器传递给指令的初始值