Polymer-AngularJS two-way data binding

二次信任 提交于 2019-12-01 11:18:01

If you tell it to, Polymer will reflect model changes back out to the published property (its attribute), but issue is that Angular doesn't observer bindings to attributes.

There's a patch that makes this work like you want: https://github.com/eee-c/angular-bind-polymer

More info here: http://blog.sethladd.com/2014/02/angular-and-polymer-data-binding.html

I started the ng-polymer-elements project which lets you have two-way binding between web components and Angular in an Angular-like way:

<input ng-model="model"/>
<paper-input ng-model="model"></paper-elements>

It comes with support for Polymer core and paper elements, and can be configured for any web component.

I belive this is what youre looking for simple and transparent 2 way data binding and capability to expand to more custom elements and for javascript not dart

NG Polymer Elements

This is my working solution, ng-polymer-elements doesn't work for me ($dirty, $pristine, etc. not working). This is very straighforward IMO

angular.module 'tinizen.admin.ui'
.directive 'paperInput', ->
  restrict: 'E'
  require: 'ngModel'
  link: (scope, elem, attrs, ctrl)->

    watcher = ->
      if ctrl.$dirty then ctrl.$invalid else false

    scope.$watch watcher, (invalid)->
      elem[0].invalid = invalid

    updateModel = (inputValue)-> ctrl.$setViewValue inputValue

    ## attrs.$observe 'inputValue', updateModel not working
    ## so I have to use on 'input'
    elem.on 'input', ->
      scope.$apply ->
        updateModel elem.prop('inputValue')

    updateModel()

    ctrl.$render = ->
      elem.prop 'inputValue', ctrl.$viewValue

according to their documentation, when binding to native elements, you have to add an extra binding notation

https://www.polymer-project.org/1.0/docs/devguide/data-binding.html#two-way-native

Here {{name}} will update on input events, the {{age}} only on the change event

<polymer-element name="x-input" attributes="name">
    <template>
        <input type="text" value={{name::input}}> <span>{{name}}</span>
        <br />
        <input type="range" value={{age::change}} > <span>{{age}}</span>
    </template>
 </polymer-element>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!