ng-model不在表中工作

时间:2016-11-14 21:53:28

标签: angularjs

我在内部或表格的文本区域内有一个ng-model(注释)。当我打印出范围时,我一直未定义。这是我的代码

<table>
    <thead>
        <tr>
            <th>Name</th>
            <th>Message</th>

        </tr>
    </thead>
    <tbody>
        <tr ng-repeat="message in messages">
            <td ng-bind="message.message"> </td>
            <td ng-bind="message.user"> </td>

            <td> <button ng-if= "message.user == name" 
                ng-click="delete(message._id)">Delete</button></td>
                <td><textarea ng-model = "comment"></textarea></td>
            <td><button ng-click="enter_comment(message._id, name)">Add Comment</button></td>
            </tr>
        </tbody>
    </table>

这是我的控制器

app.controller('welcomeController', ['$scope', '$location', 'welcomeFactory',function($scope, $location, welcomeFactory){
    index = function(){
        welcomeFactory.get_name(function(returned_data){
            $scope.name = returned_data.data.user

        })
    }
    getMessages = function(){
        welcomeFactory.get_messages(function(returned_data){
            $scope.messages = returned_data.data.message
        })
    }
    index();
    getMessages();
    $scope.enter_message = function(){
        welcomeFactory.new_message($scope.message, $scope.name, function(sendUser){
            console.log(sendUser)
            index();
            getMessages();
        })
    }
    $scope.enter_comment = function(message_id, name){
        console.log($scope.comment)
        welcomeFactory.new_comment(message_id, name, $scope.comment ,function(sendMessage){
            index();
            getMessages();
        })
    }

}]);

有解决方法吗? ng-model不在表中时工作,但在表中没有时,

1 个答案:

答案 0 :(得分:0)

查看代码$ scope.messages不是一个数组,但在html中你试图迭代消息

还试试console.log(returned_data.data.message)是否显示任何值?