1

I'm trying to submit a simple form with input in a modal (popup) written in AngularJs:

myApp.run(function($rootScope, $modal) {

    $rootScope.$on('$stateChangeStart', function(event, toState) {

        if (toState.name == 'statePopup') {

            $modal.open({
                templateUrl : 'popup.html',
                controller : 'AllegatiController',
                backdrop: 'static'
            });

            event.preventDefault();

        } else {

            return;
        }
    })
})

The form html is :

<form>
    <div class="form-group">
        <label>File Name:</label> 
        <input type="text"  ng-model="name"></input>
    </div>
    <button ng-click="save()" class="btn btn-primary">Save</button>

</form>

in my controller.js : the $scope.name came undefined

Updated:

This is my controller:

function myController($scope) {
    console.log("all myController");


     $scope.save = function() {

        var name= $scope.name;
        console.log('name is '+name);


    }
}

Maybe seems that in the run of the model which is defined for all the app missed the $scope parameter ?

What am i missed in my code?

4
  • where is you controller? where is save() method? Commented Sep 12, 2017 at 15:44
  • i updated my question with the controller Commented Sep 12, 2017 at 15:55
  • Check this demo: plnkr.co/edit/wJzt73GF9vdfi5vjTmi7?p=preview Commented Sep 12, 2017 at 15:57
  • ok , but i want to define my modal in the run function of the app in order to ensure the modal will open from anywhere in the app. I think that it missed the scope attribut in the modal but i don't know how to add it Commented Sep 12, 2017 at 16:01

2 Answers 2

1

Issue solved by updated my ui-bootsrap version up from 0.12.0

This is a link that helped me:

Link angularJs modal submit form

Sign up to request clarification or add additional context in comments.

Comments

0

If you have this:

$modal.open({
    templateUrl : 'popup.html',
    controller : 'AllegatiController',
    backdrop: 'static'
});

your controller should be:

myApp.controller('AllegatiController', ['$scope', function ($scope) {
    console.log('all myController');

    $scope.save = function () {
       var name = $scope.name;
       console.log('name is '+name);
    }
}]);

1 Comment

yes it's like this but with a different definition , the app can recognize the controller, the issue that when i submit the form in the modal, the value doesn't pass to the controller

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.