admin管理员组文章数量:1402226
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
?
2 Answers
Reset to default 6 +25I 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.
本文标签: javascriptTwoway data binding to an ngReact componentStack Overflow
版权声明:本文标题:javascript - Two-way data binding to an ngReact component - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744283090a2598749.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论