【问题标题】:Accessing a non-editable hidden field in Angular访问 Angular 中不可编辑的隐藏字段
【发布时间】:2014-04-25 19:25:26
【问题描述】:

所以我有一个独特的情况。我有一个小角度应用程序,它位于旧版 ASP.NET 网络表单应用程序(页眉和页脚)中。用户仍然通过 Webforms 进行身份验证,目前没有实现令牌服务。

但是,当 Angular 提交表单时,后端需要 JSON 包含一个等于 CurrentUserId 的 CreatedBy 值。该值可通过如下所示的隐藏字段访问: <input type="hidden" name="ctl00$contentPage$CurrentUserId" id="CurrentUserId" value="326">

另一个曲线球是这个隐藏的输入标签位于<div ng-app> </div> div 之外。

我对如何做到这一点感到有些困惑。

有什么想法吗?

【问题讨论】:

  • 为什么不只是document.querySelector('#CurrentUserId').value ???
  • 是的!太容易了,太难了。

标签: asp.net angularjs authentication


【解决方案1】:

您可以在页面上将此值注册为常量:angular.module('myApp').constant('currentUserId', '<%= CurrentUserId.Value %>');

【讨论】:

  • 谢谢!但是,您将如何在控制器中访问它?假设我需要发布具有此值的表单。我会使用类似$rootScope.currentUserId 的东西吗?
  • 将其添加为控制器中的依赖项,即function ($scope, currentUserId){} “常量”是服务的语法糖。希望对您有所帮助。
  • 只是出于好奇。与标准的document.getElementById(CurrentUserId).value 相比,使用这种方法有什么优势?
猜你喜欢
  • 2012-02-17
  • 2017-02-02
  • 1970-01-01
  • 2016-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多