I'm trying to create a native web component for input element. I wanted the component to have custom validation functionality, similar to polymer's paper-input custom validator function. I'm not sure if I can pass a custom validator function as attribute to an instance of (web component) input element. Any suggestions would be appreciated.
相关问题
- Shared styling with custom HTML elements?
- How to get Polymer 2.0 ES5 elements working with v
- Extending native HTML element in Angular 6
- How to apply global css to web components through
- webcomponents-lite with ES6 doesn't work in IE
相关文章
- Vanilla Web Component custom event attributes and
- Polymer 2.0: Notify and reflect to attribute
- Ionic 4 nav component WITHOUT Angular
- How to spawn Angular 4 component inside a Leaflet
- How do I access child elements within riot.js
- Typescript error “Property does not exist on type
- Create custom elements v1 in ES5, not ES6
- React, Uncaught ReferenceError: ReactDOM is not de
It is best to pass in the function through a property since all attributes are strings.
If you must pass it in as an attribute then you would need to convert that string into the function. The problem then become the scope of that function.
If you assume that the function is global in scope then you can use the string as a property name of the
window
object. TO execute the code you could do this:But that is pretty limiting since you might want to call a member function of your own class or object.
You could use a dot notation in the name like
func="myObj.fnName()"
and if you are not worried about the warnings of usingeval
you could just do something like this:eval(el.getAttribute('func'));
Of course that leave you open for all kinds of possible injection attacks. But, then again so does the
img
tag and thescript
tag.You could try to be safer and do this:
Set the attribute without the
()
:Your code that attempts to make the call would look like this:
But this also prevents you from passing in any parameters.
This complexity is exactly why things like AngularJS, React, Vue, etc exist. This is also the reason why I avoid passing in a function through an attribute.
If you pass it in through a property then you code can look like this:
Or anything else you want.
EVENTS
Now having said all of that I would also suggest doing what most of the native components do. That is to dispatch an event when something needs to be done, or when something changes and the outside world might be interested in those changes.
In you element you just call
Then anyone that cares about your event will listen for it.
Just pass it to the constructor. Your custom element statement:
And then instantiate your component via the
new
operator instead of thedocument.createElement
.Instantiation:
If you want to pass a function to the component, it must mean that you instantiate it via javascript anyway.
An attribute is a string, not a function. You can pass a a function as a string and then evaluate it with the
eval()
function. It's not considered as a good practice, for security reasons.Another solution is to pass it to the custom element as a Javascript property:
Or, using a arrow function: