jameskleeh / angular-confirm

Confirmation modal dialog for AngularJS
Apache License 2.0
150 stars 75 forks source link

returning a user-selected value from $confirm #44

Closed hkong closed 8 years ago

hkong commented 8 years ago

First, let me thank you for writing a good directive/service that is simple to use---solves 95% of my need for dialog boxes.

However, I have one use-case where I have this in my controller

$scope.name="Anne";
$scope.showTemplatedService = function()
{
    var data1 = {list: ["Anne","Betty","Cathy"] };
    $confirm( data1, {templateUrl: 'dialog.html'})
        .then(function() {
            alert("You selected "+ $scope.name  );
        });
}

and in a html file

...
<script type="text/ng-template" id="dialog.html">
...
    <select ng-model="name">
            <option ng-repeat="item in data.list" value="{{item}}">{{item}}</option>
    </select>
    <div id="selected"> {{name}} </div>
</script>

I can see the selection I make in the div in the dialog box after I make a selection, but how do I get that value in the promise in the controller?

Thanks in advance.

jameskleeh commented 8 years ago

The ok function can accept an argument. You can pass your name there. <button class="btn btn-primary" ng-click="ok(name)">{{data.ok}}</button>

The data will be available as a parameter to the .then function

$confirm( data1, {templateUrl: 'dialog.html'})
        .then(function(name) {
            alert("You selected "+ name  );
        });

In addition you are not using select correctly. <select ng-options="item in data.list" ng-model="name" /> is the correct usage

hkong commented 8 years ago

Wow, thanks @Schlogen, this just made a great library that much more useful! You may want to add this to one of your examples, since it wasn't intuitive it can do this. Thanks again!

jameskleeh commented 8 years ago

@hkong Ya that feature was added after I did the demo page. Any PRs will be welcome if you'd like to add it. It's on the gh-pages branch.

hkong commented 8 years ago

Sure, I'll do that when I get a chance later tonight or tomorrow.