Controller cant access ng-model inside Ionic Modal


#1

I have injected a modal by angular’s script loading technique inside a view that has one controller . Now I have a model inside this modal . But I am not able to access this model inside the controller of the whole view . Is it mandatory that I should specify a seperate controller for the modal . Please help .???


#2

No, you don’t NEED a new controller for the modal. If you create the modal like this :

 $ionicModal.fromTemplateUrl('modal.html', function(modal) {
    $scope.modal = modal;  /// HERE YOU ARE TELLING THE MODAL TO USE THE SAME SCOPE ALL THE CONTROLLER
  }, {
    // Use our scope for the scope of the modal to keep it simple
    scope: $scope,
    // The animation we want to use for the modal entrance
    animation: 'slide-in-up'
  });

you will have access to the $scope in the modal.


#3

Yea that is supposed to work that way , but for me I have a ng-click and a ng-model under the same controller . THe ng-click works but the cant access the model from that controller . When I specify a seperate controller for the modal , it works as expected , But I dont think thats required . Its just complicating things … Thanks in advance …


#4

Are you using “dot notation” on your main controller (the one that instantiates the modal)?


#5

Yea I am doing it that way .


#6

Does that affect the availabality of scope under the controller . Please Help !!!


#7

Without dot notation (objects vs. primiitives), you will often struggle with Angular due to directives that have their own child scopes or isolate scopes. Always use dot notation is a good rule of thumb.

To be more help, we’ll really need a CodePen sample. Try forking on like this and put your code in it.

Be sure to change the Ionic includes to whatever version you are using.


#8

I am basically having a angular datepicker inside the modal . I tried specifying model as

ng-model="data.dt"

but when I try to access the value from the controller I cant do it . Is’nt $scope.data supposed to have the value .


#9

Haaaa… Finally got it right . I was supposed to initialize the model like

$scope.Day={fordate:""};

That worked. Thank you …!!!