最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Two-way data binding to an ngReact component - Stack Overflow

programmeradmin5浏览0评论

Using ngReact, how does one elegantly set up a two-way data binding?

Let's say I have a simple React input ponent, which takes a value and fires onChange:

angular.module('app', []).value('SimpleInput', props => 
  <input type='text' 
         value={props.value}
         onChange{e => props.onChange(e.target.value)} />
)

Then from the AngularJS side, I would expect something like this to update value in the scope:

<react-ponent name="SimpleInput" 
                 props="{value: value, onChange: v => value = v}">
</react-ponent>

However, is there a more elegant way to set up the two-way binding to the AngularJS scope, akin to ng-model?

Using ngReact, how does one elegantly set up a two-way data binding?

Let's say I have a simple React input ponent, which takes a value and fires onChange:

angular.module('app', []).value('SimpleInput', props => 
  <input type='text' 
         value={props.value}
         onChange{e => props.onChange(e.target.value)} />
)

Then from the AngularJS side, I would expect something like this to update value in the scope:

<react-ponent name="SimpleInput" 
                 props="{value: value, onChange: v => value = v}">
</react-ponent>

However, is there a more elegant way to set up the two-way binding to the AngularJS scope, akin to ng-model?

Share Improve this question edited Apr 29, 2019 at 22:55 P Varga asked Jul 9, 2015 at 22:48 P VargaP Varga 20.3k13 gold badges77 silver badges118 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6 +25

I don't think so. ngReact is merely a means to inject React ponents into an Angular framework; React was specifically designed to not have two-way data binding in favor of performance, so any implementation of that would necessarily be a work-around.

From the horse's mouth:

ngReact is an Angular module that allows React Components to be used in AngularJS applications. Motivation for this could be any of the following: You need greater performance than Angular can offer (two way data binding, Object.observe, too many scope watchers on the page) ...

I don't have much experience with ngReact, but the React way of doing it is to use refs, and fetch the value from the ref when you need it. I'm not sure what you ponent code looks like, so I can only guess. If you have an input field inside the ponent, do this:

var SimpleInput = React.createClass({

accessFunc: function(){
    //Access value from ref here.
    console.log(React.findDOMNode(this.refs.myInput).value);
},

render: function(){
    return (
        <input type="text" ref="myInput" />
    )
  }
})

However, you can also bind the value to a state variable using linkState: https://facebook.github.io/react/docs/two-way-binding-helpers.html

However, I would strongly remend using the first way, because one of the reasons React is so much faster than Angular is because it avoids two way binding. Still, here's how:

var SimpleInput = React.createClass({

getInitialState: function(){
    return {
        myInput: ''
    }
},

render: function(){
    return (
        <input type="text" valueLink={this.linkState('myInput')}/>
    )
  }
})

Now any time you access this.state.myInput, you'll get the value of the input box.

发布评论

评论列表(0)

  1. 暂无评论