AngularJS (Material) - Refresh data in md-list after adding new object using md-dialog

I'm new to the whole Angular world, but I ran into a problem yesterday that I can't fix. As a prototype, I am creating a simple Task application that allows the user to create, view, and delete tasks. A new task can be created by clicking a button that opens a dialog. Some information can be provided and by clicking "add task" the task will be added to the database.

The only problem is that after closing the md-list dialog that displays the list of items, it is not updated to show the newly added task. I tried using the "tack by" option for ng-repeat, but it didn't work.

I got information from this question: http://stackoverflow.com/questions/27874976/update-a-md-list-dynamically-from- angular

The code I am using to display tasks is one (simplified)

<html lang="en" ng-app="taskApp">
<head></head>
<body>
    <div ng-controller="TaskController">
        <md-list>
            <md-list-item ng-repeat="task in tasks track by task.id">
                <md-checkbox ng-model="task.checked" ng-change="updateTask(task)" aria-label="Complete Task"></md-checkbox>
                <p>{{ task.title }}</p>
            </md-list-item>
        </md-list>
    </div>
</body>
</html>

      

The template for the dialog looks like this:

<md-dialog aria-label="Create new task">
    <md-content>
        <md-card class="card-padding">
            <form ng-submit="addTask(newTitle)">
                <h2 class="md-title">Add a new task</h2>
                <div layout="row">
                    <md-input-container flex>
                        <label>Title</label>
                        <input ng-model="newTitle">
                    </md-input-container>
                </div>
                <div layout="row">
                    <md-input-container flex>
                        <md-button class="md-raised md-primary" type="submit" ng-disabled="!newTitle || !newDescription">Add Task</md-button>
                    </md-input-container>
                </div>
            </form>
        </md-card>
    </md-content>
</md-dialog>

      

And the controller looks like this:

(function(angular) {
    var TaskController = function($scope, $mdDialog, Task) {
        Task.query(function(response) {
            $scope.tasks = response ? response : [];
        });

        $scope.addTask = function(title) {
            new Task({
                title: title,
                checked: false
            }).$save(function(task) {
                $scope.tasks.push(task);
            });
            $scope.newTitle = "";
            $mdDialog.hide();
        };

        $scope.showDialog = function(ev) {
            $mdDialog.show({
                controller: TaskController,
                templateUrl: 'taskdialog.tmpl.html',
                parent: angular.element(document.body),
                targetEvent: ev,
            });
        };

        $scope.updateTask = function(task) {
            task.$update();
        };

        $scope.deleteTask = function(task) {
            task.$remove(function() {
                $scope.tasks.splice($scope.tasks.indexOf(task), 1);
            });
        };
    };

    TaskController.$inject = ['$scope', '$mdDialog', 'Task'];
    angular.module("taskApp.controllers").controller("TaskController", TaskController);
}(angular));

      

So I was wondering if anyone can help me with this problem.
Thanks in advance!

+3


source to share


1 answer


you are running a task in the task list in the wrong scope.

The following should get your job done. showing dialogue.

$mdDialog.show({
  controller: TaskController,
  templateUrl: 'taskdialog.tmpl.html',
  parent: angular.element(document.body),
  targetEvent: ev,
}).then(function(task){
  $scope.tasks.push(task);
});

      



hiding the dialogue.

$mdDialog.hide(task);

      

+3


source







All Articles