【问题标题】:Html to $scope or $rootScope?Html 到 $scope 或 $rootScope?
【发布时间】:2016-06-03 21:30:59
【问题描述】:

想象一下,在一个 HTML 页面中,我有以下输入,但我没有使用 controllerAs。

<input ng-model="password" type="password" placeholder="Password">

我在哪里可以访问 $scope 或 $rootScope 中的 password 值?

我可以指定要输入哪些型号信息?

【问题讨论】:

标签: angularjs scope rootscope


【解决方案1】:

目前您正在引用 $scope。对于定位 $rootScope,您应该使用:

<input ng-model="$root.password" type="password" placeholder="Password">

【讨论】:

  • 我现在测试了,我举例说明并意识到您可以在 ($scope 和 $rootScope) 中访问模型。
  • $scope 继承自 $rootScope。
  • 我猜你的 html sn-p 不在 Controller 内,就在 ng-app 内。
【解决方案2】:

要将变量绑定到 rootScope,它由您可以使用的每个 Angular 应用程序的 ng-app 指令提供:

<input ng-model="$rootScope.password" type="password" placeholder="Password">

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多