【问题标题】:mdDialog does not close and does not update posts Angular MaterialmdDialog 不关闭也不更新帖子 Angular Material
【发布时间】:2016-03-18 02:16:04
【问题描述】:

我已将一个表单移动到一个 mdDialog 弹出窗口中,以获取用户输入以将帖子添加到页面。

我遵循了一个关于 Angular Material 的示例。到目前为止,它不会在填写表格后关闭对话框,进而不会更新我在页面上的帖子列表:

我的控制器如下所示:

myApp.controller('PostsController', ['$scope', 'PostsService', '$mdDialog',
    function ($scope, PostsService, $mdDialog) {
        $scope.posts = PostsService.getPosts()
        $scope.incrementUpvotes = function (post) {
            post.upvotes += 1;
        };
        $scope.showAdd = function (ev) {
            $mdDialog.show({
                controller: DialogController,
                scope: $scope,
                template: '<md-dialog aria-label="Mango (Fruit)"> <md-content class="md-padding"><h1>Add a Post</h1> <form name="postForm" novalidate ng-submit="addPost()"> <div layout layout-sm="column"> <md-input-container flex> <label>Title</label> <input type="text" name="title"class="form-control" ng-model="newPost.title" ng-required> </md-input-container> </div> <div layout layout-sm="column"> <md-input-container flex> <label>Date</label> <input type="date" name="date" class="form-control" ng-model="newPost.date"> </md-input-container> <md-input-container flex> </div> <div layout layout-sm="column"> <md-input-container flex> <label>Link</label> <input type="url" class="form-control" ng-model="newPost.link"> </div> <div layout layout-sm="column"> <md-input-container flex> <label>Username</label> <input type="text" name="username"class="form-control" ng-model="newPost.username" ng-required> </div> </md-input-container> </form> </md-content> <div class="md-actions" layout="row"> <span flex></span> <md-button type="submit" class="btn btn-primary" ng-click="addPost()" ng-disabled="postForm.$pristine || (postForm.$dirty && postForm.$invalid) "> Post </md-button></div> </md-dialog>'
                ,
                targetEvent: ev
            });
        };
    }]);

function DialogController($scope, $mdDialog) {
    $scope.addPost = function () {
        $mdDialog.addPost.then(function () {
            var new_id = 1 + $scope.posts[$scope.posts.length - 1].id
            $scope.posts.push({
                title: $scope.newPost.title,
                id: new_id,
                link: $scope.newPost.link,
                username: $scope.newPost.username,
                comments: [],
                upvotes: 0
            });
            $scope.newPost = {};
            $mdDialog.hide();
        });
    };
};

在我看来调用它的html如下:

<md-button class="md-fab md-fab-bottom-right" aria-label="Add" ng-click="showAdd($event)">
            <ng-md-icon icon="add"></ng-md-icon>
        </md-button>

过去几个小时我一直在玩这个,但无法解决,任何见解将不胜感激。

【问题讨论】:

    标签: angularjs angular-material


    【解决方案1】:

    你做错了我的朋友。当您在 DialogController 中调用 .hide() 方法时,$mdDialog 返回 promise。因此,将对象作为参数传递并捕获它是.then,因为它返回承诺并使用该对象推送到您的$scope.posts 对象。

    请参阅以下示例以了解 wokring 演示。 http://codepen.io/next1/pen/ONWzrE

    【讨论】:

    • 感谢您的反馈,也感谢您提供的示例,我现在可以在我的应用程序中使用它。我是 javascript 新手!
    • 如果答案正确,您可以接受并点赞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-04
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 2016-07-16
    • 1970-01-01
    • 2018-03-03
    相关资源
    最近更新 更多