2

I am using angular.ui.bootstrap modal to show a form in a modal. Everything is working fine, except when I close the modal, I want a table on the parent page to be updated with the new item added in the modal.

My parent controller:

module MyApp.Controllers {

export interface IController {
    items: Models.IItem[];
}

export interface IControllerScope {

}

export class Controller implements IController {
    items: Models.IItem[];

    constructor(
        private $scope: IControllerScope,
        private $modal: ng.ui.bootstrap.IModalService) {

    }

    addItem() {
        var options: ng.ui.bootstrap.IModalSettings = {
            templateUrl: '/directives/formFirective',
            controller: 'formController',
            controllerAs: 'modal',
            resolve: {
                id: () => 1234
            }
        };

        this.$modal.open(options).result
            .then(function (item) {
                // How do I add item to the instance variable items here?
                this.items.push(item) // Does not work here :(
                console.log(item)
            });
    }
}

}

Then in the formController I have:

this.$modalInstance.close(item);

My question is how to add the item to the instance variable items on the parent controller on modal close?

Thanks.

4
  • that looks correct. Maybe you need to add items to the $scope? Do you get a console.log from the then-function? Commented Sep 4, 2015 at 6:47
  • I fixed it by removing the function like this......then((item) => this.updateList(item)) Commented Sep 4, 2015 at 6:53
  • Hi, Bill I have a very similar issue. I just wonder how do you access the Id 1234 that you are passing in the other controller. Can you please post your code including the template as a sample please? Commented Dec 5, 2015 at 10:53
  • Hi user1829319, You just inject it into the constructor, the same way you would inject anything. Like this 'constructor( private id: number, ) { }' Commented Dec 8, 2015 at 3:12

1 Answer 1

1

try to change =>

function (item) {
                // How do I add item to the instance variable items here?
                this.items.push(item) // Does not work here :(
                console.log(item)
            }

in to:

(item) => {
                // How do I add item to the instance variable items here?
                this.items.push(item) // Does not work here :(
                console.log(item)
            }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.