javascript – 提交按钮不工作AngularJS
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – 提交按钮不工作AngularJS,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2352字,纯文字阅读大概需要4分钟。
内容图文
![javascript – 提交按钮不工作AngularJS](/upload/InfoBanner/zyjiaocheng/818/f0de19e1ea204e12901994041276cb39.jpg)
所以基本上我要做的是输入两个数字n1和n2,然后使用angular打印它们的总和.我用过bootstrap来造型.但是我注意到没有发生任何事情,所以检查我在要调用的函数中添加了alert()函数,但仍然没有以某种方式访问??它.我不懂jQuery.
PS:当我使用text1 text2时,它会整合字符串而不是打印总和
这是我的HTML:
<!DOCTYPE html>
<html ng-app="store">
<head >
<title>Trying Angular</title>
<link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap-theme.css">
<link rel="stylesheet" type="text/css" href="bootstrap/dist/css/bootstrap.min.css">
</head>
<body>
<form class="form-inline" style="border: 2px solid blue; max-width:500px;" ng-controller="formCtrl as formCtrl" ng-submit="formCtrl.submit()">
<div class="form-group">
<label>Enter n1</label>
<input type="text" class="form-control" placeholder="enter the first number" ng-model="text1">
<p>{{ text1}}</p>
</div>
<div class="form-group">
<label>Enter n2</label>
<input type="text" class="form-control" placeholder="enter the second number" ng-model="text2">
<p >{{text2}}</p>
</div>
<div class="form-group" style="display:block; margin:10px auto; margin-left:370px;">
<input type="submit" class="form-control" >
</div>
<p> Your SUM of two numbers is ={{text1+text2}}</p>
</form>
<script src="angular.min.js"></script>
<script src="exp1.js"></script>
<script src="jquery.min.js"></script>
<script src="bootstrap/dist/js/bootstrap.min.js"></script>
</body>
</html>
这是我的角度代码:
(function(){
var app=angular.module('store',[]);
var n1=0;
var n2=0;
app.controller('formCtrl',function(){
this.submit=function(){alert("successful");}; // <----- alert()
});
})();
解决方法:
如documentation for the ngController directive中所述,有两种方法可以访问控制器的成员:
>使用as< scopeProperty>语法,并使用< scopeProperty>访问它名称:
<form class="form-inline" style="border: 2px solid blue; max-width:500px;" ng-
controller="formCtrl as fc" ng-submit="fc.submit()">
这里,fc被声明为控制器实例的名称,并且使用fc访问其属性.
>将$scope注入控制器并为其添加属性:
app.controller('formCtrl', ['$scope', function($scope){
$scope.submit = function(){alert("successful");};
}]);
<form class="form-inline" style="border: 2px solid blue; max-width:500px;" ng-
controller="formCtrl" ng-submit="submit()">
这里,控制器的属性被添加到$scope,这允许我们直接在HTML中访问它们而不使用点符号.
与上面链接的页面比较和对比了这两种方法.你并没有完全使用它们,这就是你遇到麻烦的原因.
内容总结
以上是互联网集市为您收集整理的javascript – 提交按钮不工作AngularJS全部内容,希望文章能够帮你解决javascript – 提交按钮不工作AngularJS所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。