Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularModalService: passing data to controller

Not getting much w/ the search. I found one kinda similar thread here but it didn't quite work for me.

The examples show that the Modal will get a separate controller. Looks something like this.

var app = angular.module('myApp', ['ngSanitize','angularModalService']);
app.controller('SampleController', ['$scope', function($scope) {

    $scope.showAlert = function() {

        ModalService.showModal({
            templateUrl: "alertWindow.html",
            controller: "AlertController",
            inputs: {
                title: "Add New Alert",
            }
        }).then(function(modal) {
            modal.element.modal();
            modal.close.then(function(result) {
                $scope.result = "blah";
            });
        });

     });
}]);

app.controller('AlertController', ['$scope', function($scope) {

    $scope.close = function(result) {
        close(result, 500); 
    };
}]);

The caveat I have is that I want the modal dialog to have some default values which are in SampleController. I read up on Services but I don't think that works here. inputs: { } in showModal() looks suspect, but I'm not exactly sure where to get it from once the modal window is shown. The other examples I've seen just bring up simple yes/no buttons or text inputs w/ empty default values.

like image 674
kiss-o-matic Avatar asked Mar 17 '23 19:03

kiss-o-matic


1 Answers

According to the docs, you inject the inputs into the modal controller. Since title is the input you mentioned above, you would change your modal controller to:

app.controller('AlertController', ['$scope', 'title', function($scope, title) {
    $scope.title = title;

    $scope.close = function(result) {
        close(result, 500); 
    };
}]);
like image 120
nairys Avatar answered Mar 29 '23 01:03

nairys