Val Saven Val Saven - 4 months ago 12
AngularJS Question

How to send data to UI Bootstrap Modal window $scope?

I'm trying open modal window, send data from the SQL query there and change form's values, but it doesn't work. I think I should send it to modal scope someway, but I have no idea how to do that.

js file:

addUser() {
return this.Server.getUsers()
.then((res) => {
this.users = res.users;
this.$modal.open({
templateUrl: 'user.html'
});
});


And jade-file:

script(type="text/ng-template", id="users.html")
div.page-header: h1 !{__('Users')}
div.panel.panel-default
div.panel-heading
h3.panel-title !{__('Users')}
table.table.table-hover.table-condensed
thead
tr
th.col-lg-2 !{__('Name')}
th.col-lg-5 !{__('Description')}
tbody
tr(ng-repeat="u in users.users")
td {{u.name}}
td {{u.description}}

Answer

The solution:

   addUser() {
        return this.$modal.open({
            templateUrl: 'user.html',
            controller: ['$scope', 'Server', function ($scope, Server) {

                $scope.users = Server.getUsers()
                    .then((res) => {
                        this.users = res.users;
                    });
            }],
            controllerAs: "add_user"
        }).result.then((res) => {
            return console.log("Saved");
        });
    }