首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ngReact中实现深度监视

如何在ngReact中实现深度监视
EN

Stack Overflow用户
提问于 2015-07-07 15:33:25
回答 1查看 675关注 0票数 2

目前,我正在尝试将AngularJS应用程序与React集成。

我使用下面的库https://github.com/davidchang/ngReact

通过使用watch-depth,我希望当AngularJS的作用域数据发生变化时,React组件会重新呈现自己。

我的代码如下所示

index.html

代码语言:javascript
复制
<!-- index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>Hello React</title>
    <script src="bower_components/angular/angular.js"></script>
    <script src="bower_components/react/react.js"></script>
    <script src="bower_components/jquery/dist/jquery.min.js"></script>
    <script src="bower_components/ngReact/ngReact.min.js"></script>
    <script src="build/commentbox.js"></script>
</head>
<body>

<script>
    var app = angular.module('myApp', ['react']);
    app.controller('myCtrl', function($scope) {
        $scope.firstName= "John";
        $scope.lastName= "Doe";

        setTimeout(function() { 
            alert('assign data! how to trigger react component?');
            $scope.data = {data : [
                {author: "Pete Hunt", text: "This is one comment"},
                {author: "Jordan Walke", text: "This is *another* comment"}
            ]};       
        }, 5000);
    });
    app.value('CommentBox', CommentBox);
</script>

<div ng-app="myApp" ng-controller="myCtrl">
    First Name: <input type="text" ng-model="firstName"><br>
    Last Name: <input type="text" ng-model="lastName"><br>
    <br>
    Full Name: {{firstName + " " + lastName}}
    <react-component name="CommentBox" props="data" watch-depth="reference"/>
</div>

</body>
</html>

commentbox.js

代码语言:javascript
复制
// tutorial4.js
var Comment = React.createClass({
  render: function() {
    return (
      <div className="comment">
        <h2 className="commentAuthor">
          {this.props.author}
        </h2>
        {this.props.children}
      </div>
    );
  }
});

// tutorial10.js
var CommentList = React.createClass({
  render: function() {
    console.log("DEBUG");
    console.log(this.props);
    var commentNodes = this.props.data.map(function (comment) {
      return (
        <Comment author={comment.author}>
          {comment.text}
        </Comment>
      );
    });
    return (
      <div className="commentList">
        {commentNodes}
      </div>
    );
  }
});

// tutorial1.js
var CommentBox = React.createClass({
  render: function() {


    return (
      <div className="commentBox">
        <h1>Comments</h1>
        <CommentList data={this.props.data} />
      </div>
    );
  }
});

无论我使用的是watch-depth="reference"还是watch-depth="value",这都没有区别。将值赋给$scope.data时,React组件不会呈现自身

我是不是漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2015-10-07 10:48:55

使用$scope.data.push()添加新数据,并查看深度=“值”

它将重新渲染。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31262434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档