【问题标题】:Angular Schema Form - custom object editorAngular Schema Form - 自定义对象编辑器
【发布时间】:2015-02-23 11:50:35
【问题描述】:

我正在尝试使用 angular-schema-form 框架创建一个简单的“位置编辑器”。计划是构建一个带有可拖动图钉的地图编辑器小部件。

item的json schema比较简单:

location: {
    title: "Location",
    type: "object",
    format: "location",
    properties: {
        latitude: {
            "type": "number"
        },
        longitude: {
            "type": "number"
        }
    }
}

据我所知: http://plnkr.co/edit/tnSdxwrEmRYMNKFzep9f?p=preview

如您所见,编辑名称工作正常,但编辑两个位置字段中的任何一个都不会更新模型(除非您清除输入)并且验证仅发生在两个输入元素中的第二个。

我不确定下一步该往哪里看 - 我找不到对象编辑器的任何其他示例(github 上的所有示例都涉及编辑单个“字符串”项)。

这是否支持,如果支持,我哪里出错了?

干杯!

【问题讨论】:

    标签: javascript json angularjs jsonschema angular-schema-form


    【解决方案1】:

    花了一些时间之后,我设法设置了对象编辑器来映射子属性。

    在编辑器扩展代码中,我遍历架构属性将它们添加到表单对象中,然后我可以将它们映射到 Angular html 模板中。

    这是添加的 js 代码:

    var location = function(name, schema, options) {
      if (schema.type === 'object' && schema.format === 'location') {
        var f = schemaFormProvider.stdFormObj(name, schema, options);
        f.key  = options.path;
        f.type = 'location';
        options.lookup[sfPathProvider.stringify(options.path)] = f;
    
        // ADDED THIS BIT:
        // map the schema properties to form properties
        f.properties = {};
        angular.forEach(schema.properties, function(v, k) {
          var path = options.path.slice();
          path.push(k);
          if (options.ignore[sfPathProvider.stringify(path)] !== true) {
            var required = schema.required && schema.required.indexOf(k) !== -1;
    
            var def = schemaFormProvider.defaultFormDefinition(k, v, {
              path: path,
              required: required || false,
              lookup: options.lookup,
              ignore: options.ignore
            });
            if (def) {
              f.properties[k] = def;
            }
          }
        });
    
        return f;
      }
    };
    schemaFormProvider.defaults.object.unshift(location);
    

    HTML 模板(输入 type='number' 而不是 'text')现在看起来像这样:

    <div class="form-group" ng-class="{'has-error': hasError()}">
    
      <label class="control-label" ng-show="showTitle()">{{form.title}}</label>
    
          <input ng-show="form.properties.latitude.key"
                 style="background-color: white"
                 type="number"
                 class="form-control"
                 schema-validate="form.properties.latitude"
                 ng-model="$$value$$.latitude" />
    
          <input ng-show="form.properties.longitude.key"
                 style="background-color: white"
                 type="number"
                 class="form-control"
                 schema-validate="form.properties.longitude"
                 ng-model="$$value$$.longitude" />
    
      <span class="help-block">{{ (hasError() && errorMessage(schemaError())) || form.description}}</span>
    
    </div>
    

    我不知道这是否是正确的方法,但它适用于我的目的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2019-08-04
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多