1

I have two nested controller in angularjs, and want to use ng-model from outer in inner controller. Suppose this

<div ng-controller='outController'>
    // data.Name : from outer controller
    <div ng-controller='innerController'>
         <input type="text" ng-model='name' ng-init='name=data.Name'>
         {{data.Name}} // display this scope value
    </div>
 </div> 

data.Name value display in html page but not bind to name ng-model. How to bind this value to inner ng-model?

3
  • see angular guide Commented Aug 16, 2015 at 11:29
  • @Grundy prototypal inheretance could help him to implement the same thing Commented Aug 16, 2015 at 11:31
  • change ng-model to data.Name Commented Aug 16, 2015 at 11:49

1 Answer 1

4

You should follow dot rule in this case, so that will allow you to access the parent scope inside the child scope using prototypal inheritance. For using this approach you need to have an object declared in your parent controller like here it should be declared in outController then the inner controller will not create a new one, it will use the existing one using prototypal inheritance

Markup

<div ng-controller='outController'>
    // data.Name : from outer controller
  <div ng-controller='innerController'>
        <input type="text" ng-model='data.Name'>
  </div>
</div>

Code

app.controller('outController', function($scope){
   $scope.data = {};

   //..other code here ..//

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

3 Comments

i use and proces name ng-model in outer controller.
@hadi its contradiction between what you are saying.and what you written in question..
@hadi Glad to help you..Thanks :)

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.