【问题标题】:how to get value ng-model in html to javascript如何将 html 中的 ng-model 值获取到 javascript
【发布时间】:2014-11-24 14:58:52
【问题描述】:

可能是个愚蠢的问题,但我的 html 表单带有简单的输入和密码:

<li>
  <input type="text" placeholder="Username" ng-model="user.username" />
  <a class="iconani usera"></a>
</li>
<li>
  <input type="password" placeholder="Password" ng-model="user.password" />
  <a class="iconani locka"></a>
</li>

我想从 ng-model 获取价值到 java 脚本

query.equalTo("user", document.getElementById('value from ng-model'));

我从 parse.com 使用这个
你能帮帮我吗?

【问题讨论】:

  • 使用 $scope.user.username 和 $scope.user.password
  • @MatejŽvan 先生不工作:(

标签: javascript html angularjs web parse-platform


【解决方案1】:

在 AngularJS 中,您根本不需要(也不想)触摸 DOM 来获取数据。 ng-model 指令在 &lt;input&gt;$scope.user 变量的属性之间创建自动双向绑定。

login($scope.user.username, $scope.user.password, ...);

您根本不需要触摸表单本身。

【讨论】:

  • 你需要提供一个 jsfiddle/codepen,来显示你到目前为止所包含的两段代码之间的关系。
  • angular.module('AuthApp', []) .run(['$rootScope', function($scope) { $scope.scenario = '登录'; $scope.currentUser = Parse .User.current(); $scope.logIn = function(form) { Parse.User.logIn(form.username, form.password, { success: function(user) { $scope.currentUser = user; $scope.$应用();},错误:函数(用户,错误){警报(错误消息);}});};$scope.logOut =函数(表单){Parse.User.logOut();$scope.currentUser = null; };
  • 我已更新我的答案以使其更清晰(希望如此)。重要的部分是:不要在 JS 代码中触摸表单,只需使用$scope
【解决方案2】:

hon2a 的答案是正确的 ;-) 我可以尝试用不同的方式解释它,因为我最近也刚开始使用 angular。 http://www.w3schools.com/angular/angular_intro.asp 给出了关于 ng-model 和控制器的角度概念的简单介绍。

所以,你所有的 javascript 都应该在 Angular 控制器中执行。在相应的控制器(即 javascript 代码)中,来自 HTML 表单的数据使用该角度指令“ng-model”进行绑定,仅此而已。你的 HTML 部分很好,假设你在某个地方正确链接了有角度的东西(我强烈建议使用 Yeoman Angular 生成器来处理它......)。至少应该是这样的:

<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.min.js"></script>
</head>
<body>
<div ng-app="yourApp" ng-controller="yourController">
    <!-- your app part goes here -->
</div>

然后在 Angular 控制器中,您实际上会自动获得手头的数据,而无需做任何其他事情,而只是为其提供构造函数/初始化器:

angular.module('yourApp').controller('yourController', function ($scope) {

    $scope.user = {'username': '', 'userpassword': ''};

    // And rest of your stuff goes here...
    // In your functions, just use $scope.user.username and $scope.user.userpassword. 
}

希望这会有所帮助...

【讨论】:

    猜你喜欢
    • 2015-08-13
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多