【问题标题】:can't get data from ng-model using signalr无法使用信号器从 ng-model 获取数据
【发布时间】:2016-09-16 08:43:21
【问题描述】:

这是我的 .html:

 <ul class="messages list-unstyled" ng-repeat="newMessage in messages">
                    <li>
                        <p>
                            <span class="username">{{newMessage.username}}</span><br />
                        </p>
                        <p>
                            <span> {{newMessage.message}}</span>
                        </p>

                        <p class="no-pad-bottom date-posted">Send {{ newMessage.date }} <span /></p>

                        <div class="comments">
                            <ul class="list-unstyled" ng-repeat="newComment in comments">
                                <li>
                                    <p>
                                        <span class="commentor"> {{newComment.username}}</span>
                                        <span> {{newComment.message}}</span>
                                    </p>
                                    <p class="no-pad-bottom date-posted">Send {{ newComment.date }} <span /></p>
                                </li>

                            </ul>

                            <form class="add-comment">
                                <div class="row">
                                    <div class="col-md-10">
                                        <input type="text" class="form-control" ng-model="myComment" placeholder="Add a comment" />
                                    </div>
                                    <div class="col-md-2">
                                        <button class="btn btn-default btn-sm" type="submit" ng-click="addComment(newMessage.id)">Comment</button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </li>
                </ul>

一切正常。我可以发送消息,甚至可以发送帖子,但是我从 ng-model="myComment" 得到 null

这是js控制器中的方法

 $scope.addComment = function (messageId) {
        myChatHub.server.addComment(messageId, userName, $scope.myComment);
    };

你能告诉我有什么问题吗?

【问题讨论】:

    标签: angularjs signalr


    【解决方案1】:

    你打破了在ng-model .. 中总是有一个点的黄金法则,或者换句话说,使用一个对象

    ng-repeat 创建一个子作用域,并且分配给ng-model 的原语因此只存在于子作用域中,因为原语没有继承性。

    在控制器中创建模型对象

    $scope.model={}
    

    然后使用 ng-model="model.myComment" 并将您的帖子更改为:

    $scope.addComment = function (messageId) {
            myChatHub.server.addComment(messageId, userName, $scope.model.myComment);
    };
    

    This 3 minute video会很有启发性

    【讨论】:

      猜你喜欢
      • 2016-07-10
      • 1970-01-01
      • 2015-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-06
      相关资源
      最近更新 更多