Angular JS how to pass value from root directory to ng model?

I have an object in root vegetables and I would like to display some values โ€‹โ€‹in form inputs.

I've tried the following:

  <input type="number" ng-model="$root.order.id"  class="form-control" id="orderNumber" />

      

But it doesn't work.

How do I pass a value to an ng model?

Thanks for any help.

+3


source to share


2 answers


No need to attach $ root variable to variable, scope flow in angular is the first search in local scope for variable, if no property search found in $ scope.parent and rootScope if parent high level, if not match with anything else. and then search there.

http://plnkr.co/edit/3ENyPRwrFq5ssR2uLtQy

In this plnkr see the use of root scope

Controller:



app.controller('MainCtrl', ["$scope", "$rootScope", function($scope, $rootScope) {
  $rootScope.varRoot = {
    element: "Jesรบs"
  };
}]
);

      

HTML:

 <body ng-controller="MainCtrl">
    <p>Hello {{varRoot.element}}!</p>
    <input type="text" ng-model="varRoot.element">
  </body>

      

+1


source


Just use a name like:



$rootScope.order.id = 3;

<input type="number" ng-model="order.id"  class="form-control" id="orderNumber" />

      

0


source







All Articles