Deepankar Bajpeyi
Deepankar Bajpeyi

Reputation: 5859

Scope variables undefined in a controller

I am using Angular-app as the seed app for my project. I have written quite a number of pages and everything seems to work fine.

However in one of the cases where my template looks something like this :

<div ng-switch on="result">
     <div ng-switch="error">Something went wrong!, try later</div>

      <div ng-switch-default>
          Wait loading information!!!!
      </div> 

      <div ng-switch-when="ok">
           <form ng-submit="sendData()">
               <input type="text" ng-model="myName"/ >
               <button type="submit" >Create entry</button>
           </form>  
      </div>
</div>

Even if I type anything for myName I get an undefined value when I submit the form ?

Why is the scope getting lost ?

Is it because of ng-switch ? I tried putting ng-controller="MyController" on

<div ng-switch-when="ok" ng-controller="MyController">....</div>

and it works just fine !!!

Although if I try to use a bigger div on top of everything, the scope is undefined again

Upvotes: 0

Views: 348

Answers (1)

Kavitha K Gowd
Kavitha K Gowd

Reputation: 527

If you are using ng-switch the view is created during runtime and the scope is getting lost. In order hold the reference you have to use javascript reference object. Working example is here http://jsfiddle.net/J5ez6/ Hence your view will be

<form ng-submit="sendData()">
               <input type="text" ng-model="mod.myName"/ >
               <button type="submit" >Create entry</button>
           </form>

Upvotes: 1

Related Questions