Angularjs mdDialog不会关闭,也不会更新posts角材质
我已将表单移动到mdDialog弹出窗口中,以获得用户输入,从而向页面添加帖子 我举了一个关于角材料的例子。到目前为止,它不会在填写表单后关闭对话框,也不会更新我在页面上的帖子列表: 我的控制器如下所示:Angularjs mdDialog不会关闭,也不会更新posts角材质,angularjs,angular-material,Angularjs,Angular Material,我已将表单移动到mdDialog弹出窗口中,以获得用户输入,从而向页面添加帖子 我举了一个关于角材料的例子。到目前为止,它不会在填写表单后关闭对话框,也不会更新我在页面上的帖子列表: 我的控制器如下所示: myApp.controller('PostsController', ['$scope', 'PostsService', '$mdDialog', function ($scope, PostsService, $mdDialog) { $scope.posts =
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();
});
};
};
myApp.controller('PostsController',['$scope','PostsService','$mdDialog',
函数($scope,PostsService,$mdDialog){
$scope.posts=PostsService.getPosts()
$scope.IncrementUpVoces=函数(post){
post.upvoces+=1;
};
$scope.showAdd=功能(ev){
$mdDialog.show({
控制器:对话框控制器,
范围:$scope,
模板:“添加帖子标题日期链接用户名帖子”
,
目标事件:ev
});
};
}]);
函数DialogController($scope,$mdDialog){
$scope.addPost=函数(){
$mdDialog.addPost.then(函数(){
var new_id=1+$scope.posts[$scope.posts.length-1].id
$scope.posts.push({
标题:$scope.newPost.title,
id:new_id,
链接:$scope.newPost.link,
用户名:$scope.newPost.username,
评论:[],
赞成票: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>
在过去的几个小时里,我一直在研究这个问题,但无法解决,任何见解都将不胜感激。我的朋友,你做错了
$mdDialog
在调用DialogController
中的.hide()
方法时返回promise。因此,将对象作为参数传递并捕获它是。然后在它重新执行承诺时使用该对象推送到$scope.posts
对象
请参见下面的wokring演示示例。
感谢您的反馈,也感谢您的示例,我现在已经在我的应用程序中使用了它。我是javascript新手!如果答案正确,你可以接受并投票表决。