3

I have object into rootscope and i would like to display some values in form inputs.

I tried following:

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

But this is not working.

How i should pass value into ng-model?

Thanks for any help.

2 Answers 2

1

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

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

In this plnkr look the usage of the 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>
Sign up to request clarification or add additional context in comments.

Comments

0

Just use the name, ex:

$rootScope.order.id = 3;

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

Comments

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.