ng-model从指令返回'undefined'

时间:2016-01-04 20:30:35

标签: javascript angularjs

我正在尝试使用隐藏了ng-show的回复字段来实现类似reddit的嵌套注释系统。

但是当我尝试将注释从自定义指令返回到控制器时,结果总是“未定义”。

我已经按照本教程进行了操作:

  

How to get the form data when the form is in a directive in Angular?

但它不适合我。

app.directive("replyContainer", function() {
    return {
        restrict: "E",
        scope: {
            onSubmit: "&"
        },
        template: '<a ng-click="replyForm = !replyForm">reply</a>' +
          '<div ng-show="replyForm" id="replyForm">' +
            '<form ng-submit="submit()" ng-transclude'+
            'ng-show="isLoggedIn()"'+
            'style="margin-top:30px;">'+
              '<h3>Reply</h3>'+
              '<div class="form-group">'+
                '<input type="text" '+
                'class="form-control"'+
                'placeholder="Comment"'+
                'parent="{{comment._id}}"'+
                '<script>console.log(parent)</script>'
                'ng-model="model.Name"></input>'+
              '</div>'+
              '<button ng-click="submit()"'+
              'class="btn btn-primary">Reply</button>'+
            '</form>'+
            '</div>',
        transclude: true,
        link: function(scope, element, attributes) {
            scope.submit = function() {
                scope.onSubmit();
            }
        }

    }
})
app.controller('PostsCtrl', ['$scope', 'posts', 'post', 'auth',
function($scope, posts, post, auth) {

    $scope.model = {};
    $scope.submittedValue = null;
    $scope.post = post;
    $scope.isLoggedIn = auth.isLoggedIn;
    $scope.replyForm = false;

    $scope.addReply = function (){
        $scope.submittedValue = $scope.model.Name;
        console.log($scope.parent);
    };

index.ejs:

<reply-container on-submit="addReply()"></reply-container>

0 个答案:

没有答案