动态创建角度飞镖组件

时间:2014-10-11 19:04:16

标签: dart angular-dart

我正在尝试动态创建角度飞镖组件。我知道这不是最好的做法,但我必须考虑到我的角度小部件的插入方式。

我的工作基于:

How to add a component programatically in Angular.Dart?

由于Angular Dart库中的更改,代码会对较长时间的工作进行采样。

我让这段代码工作但是它不一致。解决方案是Timer.run()来触发scope.apply。问题是:

  1. 这样的电话很糟糕,而且会有很多组件执行
  2. 似乎随机工作。它大部分时间都有,但偶尔也不会进行{{foo}}替换
  3. void main() {
      IBMModule module = new IBMModule();
      AngularModule angularModule = new AngularModule();
    
      Injector injector = applicationFactory()
      .addModule(module)
      .run();
    
      AppComponent appComponent = injector.get(AppComponent);
      appComponent.addElement("<brazos-input-string label='test'/>");
    }
    
    class MyValidator implements NodeValidator {
    
    
      bool allowsElement(Element element) {
        return true;
      }
    
      bool allowsAttribute(Element element, String attributeName, String value) {
        return true;
      }
    
    }
    
    @Injectable()
    class AppComponent {
      NodeValidator validator;
      Compiler _compiler;
      DirectiveInjector _directiveInjector;
      DirectiveMap _directiveMap;
      NodeTreeSanitizer _nodeTreeSanitizer;
      Injector _appInjector;
      Scope _scope;
    
      AppComponent(this._directiveInjector, this._compiler, this._directiveMap, this._nodeTreeSanitizer, this._appInjector, this._scope) {
        validator = new MyValidator();
      }
    
      void addElement(String elementHTML) {
        DivElement container = querySelector("#container");
        DivElement inner = new DivElement();
        container.append(inner);
        Element element = new Element.html(elementHTML, validator: validator);
        // inner.setInnerHtml(elementHTML, validator: validator);
        ViewFactory viewFactory = _compiler.call([element], _directiveMap);
        if (_scope != null) {
          Scope childScope = _scope.createProtoChild();
          View newView = viewFactory.call(childScope, _directiveInjector);
          newView.nodes.forEach((node) => inner.append(node));
          Timer.run(() => childScope.apply());
        } else {
          print("scope is null");
        }
      }
    }
    
    
    class IBMModule extends Module {
      IBMModule() {
        bind(BrazosInputStringComponent);
        bind(BrazosTextAreaComponent);
        bind(BrazosButtonComponent);
        bind(ProcessDataProvider, toImplementation: ActivitiDataProvider);
        bind(AppComponent);
      }
    }
    

0 个答案:

没有答案