简体   繁体   English

模板中的AngularJS ng模型传递给指令控制器

[英]AngularJS ng-model in template passed to directive controller

I've got a directive with a controller, that builds a form for posting comments to an API via CommentsService 我有一个带有控制器的指令,该指令构建了一个表单,用于通过CommentsService将评论发布到API

My directive looks a bit lik this: 我的指令看起来有点像这样:

app.directive('appComments', function( CommentService ) {
    return {
        restrict: 'E',
        scope: {
            event: '='
        },
        controller: function( $rootScope, $scope, $element ) {

            $scope.comments = [];
            $scope.comment_text = '';

            // load comments if event ID has changed
            $scope.$watch( 'event', function() {
                if( typeof $scope.event != 'undefined' ) {
                    CommentService.get( $scope.event ).then(
                        function( comments ) {
                            $scope.comments = comments;
                        }
                    );
                }
            });

            // post comment to service
            $scope.postComment = function() {
                if( $scope.comment_text != '' ) {

                    CommentService.post(
                        $scope.event,
                        $scope.comment_text,
                        function() {
                            // code to reload comments
                        }
                    );
                }
            };
        },
        templateUrl: '/partials/comments.html'
    };
});

This is my comments.html for the directive 这是我对指令的comments.html

<div class="event-comments">
    <p ng-if="!comments.length">
        <span>This event has no comments.</span>
    </p>
    <div 
        class="event-comment" 
        ng-repeat="comment in comments"
    >
        <div class="comment-text">{{comment.text}}</div>
    </div>
</div>
<div class="insert-comment-container" ng-if="!loading">
    <form ng-submit="postComment()">
        <textarea 
            ng-model="comment_text"
        ></textarea>
        <div
            ng-tap="postComment()"
        >Post</div>
    </div>
</div>

This is how I'm placing it in my main view: 这就是我将其放置在主视图中的方式:

<app-comments event="event.id"></app-comments>

My comments are loading, and the event id is getting passed, but when I try and post a comment the comment_text is blank. 我的评论正在加载,并且事件ID正在传递,但是当我尝试发布评论时, comment_text为空白。

I think I'm getting my scopes mixed up or something, I'm still not completely clear on directives 我想我的范围混在一起了,或者我对指令尚不完全清楚

** update ** **更新**

I've just realised if I set 我刚意识到如果我定下

$scope.comment_text = 'Initial text'

in the directive, it appears when the template renders inside the textarea, and the if statement in the postComments() function fires. 在指令中,当模板在textarea内渲染时出现,并且postComments()函数中的if语句触发。 But if I change the text in the textarea, and tap the post button, the contents of $scope.comment_text is still "Initial text", it seems to be a one way binding. 但是,如果我更改文本区域中的文本,然后点击“发布”按钮,则$scope.comment_text的内容仍然是“初始文本”,这似乎是一种单向绑定。

Since you are using form i believe it creates a new scope scope. 由于您使用的是表格,我相信它会创建一个新的范围范围。 As per documentation 根据文档

If the name attribute is specified, the form controller is published onto the current scope under this name. 如果指定了name属性,则表单控制器将以该名称发布到当前作用域上。

Try to give your form a name. 尝试给您的表格起一个名字。 Or else try to pass the property as object property like 否则尝试将属性作为对象属性传递

 <textarea 
            ng-model="comment.comment_text">
 </textarea>

Ok so by changing comment_text to comment.text it solved the problem, as recommended by this SO answer: 好的,通过将comment_text更改为comment.text它可以解决此问题,如该SO答案所建议:

angular-bootstrap (tabs): data binding works only one-way angular-bootstrap(选项卡):数据绑定仅单向工作

Using an object instead of a primitive just uses the same reference to the object property, instead of copying the primitive into the new scope. 使用对象代替基本体只使用对对象属性的相同引用,而不是将基本体复制到新的作用域中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM