$ http和$ scope变量未在模板中显示

时间:2014-01-17 15:08:37

标签: javascript jquery angularjs

变量$scope.discussionId未在我的模板中显示。每隔一个值都显示

我做错了什么?

编辑(完整代码)

CONTROLLER

soundshareApp.controller('CommentController', function($scope, $http) {

    $scope.data = { comments : [] }
    $scope.state = {}
    $scope.project = { id : [] }
    $scope.discussionId;
    $scope.test = "Hello World !";


    $scope.loadComments = function(userName, urlName){
        $http({
            url: '/comment/by_project_id',
            method: "GET",
            params:
            {
                username: userName,
                urlname: urlName
            }
        }).success(function(data) {
            $scope.discussionId = data[0].discussionId;
            console.log("discussionId " + $scope.discussionId);//display the value !!
            $scope.data.comments = data;
        });;
    }
}

TEMPLATE

<div ng-controller="CommentController" >
    <div class='message-content' ng-click="state.show = !state.show">
        <img class='message-vignette' ng-src='{{commentData.avatarUrl}}'/>
        <div class='message-username'>{{commentData.username}}</div>
        <div class='project-message'> {{commentData.comment}}</div>
        <div class='message-date'>{{commentData.dateCreated | date:'MM/dd/yyyy @ h:mma' }}</div>
    </div>

    <div  class="answer-message-content show-hide" ng-show="state.show" >
        <img class='message-vignette answer-message-vignette' ng-src='{{commentData.currentUserAvatarUrl}}'>
        <div class="add-comment-content">
            <form ng-submit="addComment(commentData)"   id="commentForm-{{commentData.projectId}}">
                <input id="input-comment-{{commentData.projectId}}" type="text" maxlength="255" name="comment" placeholder="Write a comment..." ng-model="commentData.msg">
                <input type="hidden" name="discussionId" value="{{commentData.discussionId}}" >
                <input type="hidden" name="projectId" value="{{commentData.projectId}}" >
            </form>
        </div>
    </div>
    test :  {{test}}// showing "Hello World!"
    discussionId: {{discussionId}}//not  showing !!
    <div ng-repeat="answer in answers" class="answer-message-content">
        <div class='message-content' ng-click="state.show = !state.show">
            <img class='message-vignette' ng-src='{{answer.avatarUrl}}'/>
            <div class='message-username'>{{answer.username}}</div>
            <div class='project-message'> {{answer.comment}}</div>
            <div class='message-date'>{{answer.dateCreated | date:'MM/dd/yyyy @ h:mma' }}</div>
        </div>
    </div>
</div>

MAIN(Grails GSP)

<div ng-init="loadComments('${params.username}', '${params.urlname}' )">
    <div ng-repeat="pdata in data.comments" >
        <div sdcomment param="pdata"></div>
    </div>
</div>

指令

soundshareApp.directive('sdcomment', function(){
    return {
        restrict:'A',
        scope: {
            commentData: '=param'
        },
        templateUrl: '/js/views/comment/templates/commentList.html',

        link : function(scope, elem, attrs) {

        }
    }

});

谢谢

1 个答案:

答案 0 :(得分:1)

我没有看到$scope.loadComments被调用过。 $scope.discussionId;初始化未定义。如果你初始化它我打赌你会显示该值,如:

$scope.discussionId = 10;