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

时间:2016-08-08 06:54:36

标签: javascript angularjs

如何将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>

为什么在我的提交功能中它没有获得用户名的值?

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

7 个答案:

答案 0 :(得分:2)

您无需将username作为参数传递给您的函数。您可以在函数中从$ scope.username获取用户名值。

答案 1 :(得分:1)

这是正确答案:

更正了alert中的'用户名',并且在定义方法时也不应使用paranthesis $scope.submit = function() { }

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

您还在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>

答案 2 :(得分:1)

有一些错误和拼写错误。

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('TodoCtrl', function($scope) {
  $scope.submit = function(username){
  	alert(username) //get username here
    console.log("username", username)
  }
});
&#13;
<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>
&#13;
&#13;
&#13;

以下是更新的 Demo

答案 3 :(得分:0)

public class TvActivity extends Activity { // Declare variables ProgressDialog pDialog; VideoView videoview; // Insert your Video URL String VideoURL = ""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get the layout from video_main.xml setContentView(R.layout.videoview_main); // Find your VideoView in your video_main.xml layout videoview = (VideoView) findViewById(R.id.VideoView); // Execute StreamVideo AsyncTask VideoURL = "http://ns3622101.ip-149-202-201.eu:8000/live/fr443500/75019pa/286.ts"; // Create a progressbar pDialog = new ProgressDialog(TvActivity.this); // Set progressbar title pDialog.setTitle("Video Streaming "); // Set progressbar message pDialog.setMessage("buffering ..."); pDialog.setIndeterminate(true); pDialog.setCancelable(true); // Show progressbar pDialog.show(); try { // Start the MediaController final MediaController mediacontroller = new MediaController( TvActivity.this); mediacontroller.setAnchorView(videoview); // Get the URL from String VideoURL final Uri video = Uri.parse(VideoURL); videoview.setMediaController(mediacontroller); videoview.setVideoURI(video); videoview.requestFocus(); videoview.setOnPreparedListener(new OnPreparedListener() { // Close the progress bar and play the video public void onPrepared(MediaPlayer mp) { pDialog.dismiss(); videoview.start(); } }); videoview.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { videoview.stopPlayback(); videoview.setVideoURI(video); videoview.requestFocus(); videoview.start(); } }); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } } } 属性缺少相应的内容。试试这个:

ng-app

此外,更正您的函数名称和参数拼写错误:

<div ng-app='myApp'>

答案 4 :(得分:0)

您的代码中存在一些错误

下面,找到固定代码

<强> 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

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

答案 5 :(得分:0)

小提琴中有错误

请更新您提交的方法,如下所示

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

在$ scope.submit之后使用括号(),这是函数的无效声明。

还为ng-app命名,如

<div ng-app="myApp">   

并且内部警报正确地给出了变量名,你在变量名中做了拼写错误。

答案 6 :(得分:0)

这是您的要求的一个示例。只需测试我在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>