我在 rootscope 中有对象,我想在表单输入中显示一些值。
我试过以下:
<input type="number" ng-model="$root.order.id" class="form-control" id="orderNumber" />
但这不起作用。
我应该如何将价值传递给 ng-model?
谢谢你的帮助。
我在 rootscope 中有对象,我想在表单输入中显示一些值。
我试过以下:
<input type="number" ng-model="$root.order.id" class="form-control" id="orderNumber" />
但这不起作用。
我应该如何将价值传递给 ng-model?
谢谢你的帮助。
变量不需要附加de $root,angular中作用域的流程是先在本地作用域中搜索变量,如果没有找到则搜索$scope.parent中的属性,如果是父级的高层则为rootScope不匹配任何其他,然后在那里搜索。
http://plnkr.co/edit/3ENyPRwrFq5ssR2uLtQy
在这个 plnkr 中查看根范围的用法
控制器:
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>
只需使用名称,例如:
$rootScope.order.id = 3;
<input type="number" ng-model="order.id" class="form-control" id="orderNumber" />