简体   繁体   English

绑定到Aurelia中的命名空间属性

[英]Binding to a Namespaced Attribute in Aurelia

When bind to attributes(one-way) there are a couple of options you can either bind with attr-name.bind="variable" (also tried one-way and one-time) or using interpolation attr-name="${variable}" , either way though if you try to bind to a namespaced element such as xlink:href you currently get: 当绑定到属性(单向)时,有几个选项可以与attr-name.bind="variable"绑定(也尝试单向和一次)或使用插值attr-name="${variable}" ,无论哪种方式,如果您尝试绑定到命名空间元素,如xlink:href,您目前得到:

Uncaught NamespaceError: Failed to execute 'setAttributeNS' on 'Element': '' is an invalid namespace for attributes. Uncaught NamespaceError:无法在'Element'上执行'setAttributeNS':''是属性的无效命名空间。

For have the following in a controller page.js: 要在控制器page.js中包含以下内容:

export class page {
    constructor(){
        this.icon = 'blah';
    }
}

and the following in page.html: 以及page.html中的以下内容:

<template>
  <svg class="icon ${icon}">
    <use xlink:href="${icon}"></use>
  </svg>
</template>

As I said either of the bind's above is throwing the given error. 正如我所说,上面的任何一个绑定都会抛出给定的错误。

Any thoughts on how to bind to this namespaced attribute? 有关如何绑定到此命名空间属性的任何想法?

Instrumented the bootstrap handleApp function to print the full stack trace: 检测bootstrap handleApp函数以打印完整的堆栈跟踪:

Error: Failed to execute 'setAttributeNS' on 'Element': '' is an invalid namespace for attributes. at Error (native) at OoPropertyObserver.setValue (http://localhost:9000/jspm_packages/github/aurelia/binding@0.3.3/system/property-observation.js:200:26) at InterpolationBinding.setValue (http://localhost:9000/jspm_packages/github/aurelia/templating-binding@0.8.4/system/binding-language.js:214:35) at InterpolationBinding.bind (http://localhost:9000/jspm_packages/github/aurelia/templating-binding@0.8.4/system/binding-language.js:202:22) at View.bind (http://localhost:9000/jspm_packages/github/aurelia/templating@0.8.9/system/view.js:65:29) at ViewFactory.create (http://localhost:9000/jspm_packages/github/aurelia/templating@0.8.9/system/view-factory.js:173:22) at BoundViewFactory.create (http://localhost:9000/jspm_packages/github/aurelia/templating@0.8.9/system/view-factory.js:128:39) at Repeat.processItems (http://localhost:9000/jspm_packages/github/aurelia/templating-resources@0.8.6/system/repeat.js:105:36) at Repeat.bind (http://localhost:9000/jspm_packages/github/aurelia/templating-resources@0.8.6/system/repeat.js:60:22) at BehaviorInstance.bind (http://localhost:9000/jspm_packages/github/aurelia/templating@0.8.9/system/behavior-instance.js:67:39)

Also if I hack the property-observation code to explicitly set the namespace it works, but this is really kludgy and likely to break quickly. 此外,如果我破解属性观察代码以明确设置它可以工作的命名空间,但这真的很糟糕,很可能会迅速破解。

https://github.com/aurelia/binding/blob/master/src/property-observation.js#L153-L159 changed to: https://github.com/aurelia/binding/blob/master/src/property-observation.js#L153-L159更改为:

setValue(newValue) {
    if (this.isSVG) {
      if(this.propertyName.indexOf('xlink:') >= 0){
        this.obj.setAttributeNS("http://www.w3.org/1999/xlink", this.propertyName, newValue);
      } else {
        this.obj.setAttributeNS(null, this.propertyName, newValue);
      }
    } else {
      this.obj[this.propertyName] = newValue;
    }
}

Handling for namespaced elements in aurelia is now explicitly supported thanks to https://github.com/aurelia/binding/issues/34 . 现在,通过https://github.com/aurelia/binding/issues/34明确支持处理aurelia中的命名空间元素。 No need to do anything special. 无需做任何特别的事情。 This works for HTML and HTML5 issues with namespacing. 这适用于使用命名空间的HTML和HTML5问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM