简体   繁体   English

将ng-model值传递到ng-click失败

[英]Pass ng-model value into ng-click failed

How to pass ng-model's value to ng-click? 如何将ng-model的值传递给ng-click?

<div ng-controller="TodoCtrl">
  <input type="text" ng-model="username">
  <button class="btn-primary" type="submit" ng-click="submit(username)">submit</button>
</div>

Why in my submit function it doesn't get the username's value? 为什么在我的Submit函数中没有获取用户名的值?

http://jsfiddle.net/U3pVM/26604/ http://jsfiddle.net/U3pVM/26604/

You don't need to pass username as a parameter to your function. 您不需要将用户名作为参数传递给函数。 You can get the username value from $scope.username in your function already. 您已经可以从函数中的$ scope.username获取用户名值。

Here is the correct answer: 这是正确的答案:

Corrected the 'username' in alert and also while defining the method should not use paranthesis $scope.submit = function() { } 更正了alert的“用户名”,并且在定义方法时也不应使用括号$scope.submit = function() { }

var app = angular.module('myApp', []);
app.controller('TodoCtrl', function($scope) {
  $scope.submit = function(username){
    alert(username) //get username here
  }
});

Also you have specify the module name in the html: 您还可以在html中指定模块名称:

<div ng-app='myApp'>
  <div ng-controller="TodoCtrl">
      <input type="text" ng-model="username">
             <button class="btn-primary" type="submit" ng-click="submit(username)">submit</button>
  </div>
</div>

There are some missings and spelling errors. 有一些遗失和拼写错误。

 var app = angular.module('myApp', []); app.controller('TodoCtrl', function($scope) { $scope.submit = function(username){ alert(username) //get username here console.log("username", username) } }); 
 <div ng-app ="myApp"> <div ng-controller="TodoCtrl"> <input type="text" ng-model="username"> <button class="btn-primary" type="submit" ng-click="submit(username)">submit</button> </div> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js"></script> 

Here is the updated Demo 这是更新的演示

There is a missing decleration of your ng-app attribute. 您的ng-app属性缺少缺失。 Try this: 尝试这个:

<div ng-app='myApp'>

In addition, correct your function name and argument typo: 此外,更正您的函数名称和参数拼写错误:

$scope.submit = function(username){
    alert(username) 
  }

There are some errors in your code 您的代码中有一些错误

Below, find the fixed code 在下面找到固定的代码

HTML HTML

<div ng-app="myApp">
  <div ng-controller="TodoCtrl">
      <input type="text" ng-model="username">
      <button class="btn-primary" type="submit" ng-click="submit(username)">submit</button>
  </div>
</div>

CONTROLLER CONTROLLER

var app = angular.module('myApp', []);
app.controller('TodoCtrl', function($scope) {
    $scope.submit = function(username){
        alert(userame) //get username here
    }
});

there are errors in your fiddle 你的小提琴中有错误

please update your submit method as below 请如下更新您的提交方法

  $scope.submit = function(username){
    alert(username) //get username here
  }

You are using parenthesis () after $scope.submit which is invalid declaration for function. 您在$ scope.submit之后使用括号(),该声明对函数无效。

also give name to ng-app like 也给ng-app命名

<div ng-app="myApp">   

and inside alert give variable name properly , you have done spelling mistake in variable name. 并且警报内部正确地给了变量名,您在变量名中拼写错误。

This is an example of your requirement. 这是您要求的一个例子。 Just test on my snippet sending value on alert() . 只需测试我的代码片段,即可在alert()上发送值。

 var app = angular.module("myApp",[]); app.controller("myCtrl",function($scope){ $scope.submit = function(mymdl){ alert(mymdl); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="myCtrl"> <input type="text" ng-model="mymdl"/> <button ng-click="submit(mymdl)">Submit</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM