本文介绍了ng-admin无法识别自定义字段视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ng-admin.
根据 doc ,我创建了一个自定义字段.
ng-admin可以识别FooFiledType,但不会渲染FoofieldView并使用原始的FieldView!

I'm using ng-admin.
According to doc, I created a custom field.
ng-admin recognize the FooFiledType but it dosn't render the FoofieldView and use the original FieldView !

配置模块:

config module :

angular.module('admin', ['ng-admin']);

import FooField from './FooFieldType';

angular.module('admin')
  .config(['NgAdminConfigurationProvider', function (nga) {
    nga.registerFieldType('foo', FooField);
  }])
  .config(['FieldViewConfigurationProvider', function (fvp) {
    fvp.registerFieldView('foo', require('./FooFieldView'));
  }])
  ;
angular.module('admin').config(['NgAdminConfigurationProvider', function (nga) {
   var bar = nga.entity('bar');
   bar.creationView().fields([nga.field('foo','foo')]);
});

FooField.js:

FooField.js :

import Field from 'admin-config/lib/Field/Field';
class FooField extends Field {
  constructor(name) {
    super(name);
  }
}
export default FooField;

FooFieldView.js:

FooFieldView.js :

export default {
  // displayed in listView and showView
  getReadWidget   : () => '<ma-number-column field="::field" value="::entry.values[field.name()]"></ma-number-column>',
  // displayed in listView and showView when isDetailLink is true
  getLinkWidget   : () => '<a ng-click="gotoDetail()">' + module.exports.getReadWidget() + '</a>',
  // displayed in the filter form in the listView
  getFilterWidget : () => '<ma-input-field type="number" field="::field" value="values[field.name()]"></ma-input-field>',
  // displayed in editionView and creationView
  getWriteWidget  : () => '<h1>testFoo</h1><ma-input-field type="number" field="::field" value="entry.values[field.name()]"></ma-input-field>'
};

推荐答案

Doc似乎不完整...但是我将其范围缩小到字段中缺少的"this._type"设置.您需要按如下所示更改FooField.js:

Doc seems incomplete... but I've narrowed it down to a missing 'this._type' setting into the Field. You need to change FooField.js as follows:

....
class FooField extends Field {
  constructor(name) {
    super(name);
    //THIS IS THE IMPORTANT PART:
    this._type = 'foo';
    //DOC DOESN'T SPEAK ABOUT
  }
}
....

在FooField类中定义 _type 时,将调用并渲染根据类型的视图.否则,由于我们继承自"Field",因此呈现了String视图,似乎没有任何作用...

When you define _type in the FooField class then the view according to type is called and therefore rendered. Else since we are inheriting from 'Field' the String view is rendered and it seems like nothing works...

Doc确实需要进行一些更新:)

Doc really needs to get some update :)

这篇关于ng-admin无法识别自定义字段视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 22:18