角度绑定不适用于文本框?

时间:2013-10-09 10:55:08

标签: angularjs

我正面临一个字符串问题。我已经放置了一个文本输入控件并将其与控制器属性绑定并添加了一个按钮输入,但是当我尝试在按钮上获取文本输入值时,我将变为空白。

控制器

chat.controller('chatController', ['$scope', 'board', '$log', function ($scope, board, $log) {
$scope.Messages = [];
$scope.comment = '';
board.startBoard(function () {
    board.loadAllMessages().then(function (messages) {
        $scope.Messages = messages;
    });
});
$scope.likeClick = function (isfromChild, message) {
    $log.info(message);
};
$scope.dislikeClick = function (isfromChild, message) {
    $log.info(message);
};
$scope.addComment = function () {
    //HERE IS THIS PROBLEM
    alert($scope.comment);
    $scope.comment = ''; 
};
} ]);

Mark Up

 <div style="margin-top: 10px">
                                    <div class="input-group">
                                        <input type="text" class="form-control" data-ng-model="comment" placeholder="write a comment..." />
                                        <span class="input-group-btn">
                                            <button class="btn btn-info" data-ng-click="addComment()" id="button1">
                                                Post Commant</button></span>
                                    </div>
                                </div>

2 个答案:

答案 0 :(得分:1)

只需添加ng-controller属性,如下所示:

 <div style="margin-top: 10px"  ng-controller="chatController">

工作示例:http://jsfiddle.net/shPwB/1/

答案 1 :(得分:0)

请按以下更新

 <div style="margin-top: 10px" ng-controller="chatController">
                                <div class="input-group">
                                    <input type="text" class="form-control" data-ng-model="comment" placeholder="write a comment..." />
                                    <span class="input-group-btn">
                                        <button class="btn btn-info" data-ng-click="addComment()" id="button1">
                                            Post Commant</button></span>
                                </div>
                            </div>