两个 ng 单击函数不会一起执行
Two ng-click function not executing together
我想在一次ng单击中执行两个函数,但它没有执行。
当我尝试单独执行它们时,它工作正常。
我用分号 ' 分隔两个函数名称;'.
这是我的代码:
<!DOCTYPE html>
<html>
<body ng-app="starter" ng-controller="NavCtrl">
<ion-pane>
<ion-header-bar class="bar-stable">
<h1 class="title">Ionic head</h1>
</ion-header-bar>
<ion-content>
<h4>Enter the details</h4>
<div class="list list-inset" >
<label class="item item-input">
<input type="text" name="place" ng-model="data.place" value="" placeholder="Place">
</label>
<label class="item item-input">
<input type="number" name="pincode" ng-model="data.pincode" value="" placeholder="Pin Code">
</label>
<button class="button button-block button-positive" ng-click="savedata(); navigat('/success.html')" >
Submit
</button>
</div>
</ion-content>
</ion-pane>
</body>
</html>
这是控制器代码:
app.controller('NavCtrl',function($scope,$location,$http,$window){
$scope.data = {};
$scope.savedata = function(){
$http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
.success(function(data,status,headers,config){
//console.log(data);
alert("successful");
})
.error(function(){
alert("failed");
})
};
$scope.navigat = function(url){
$window.location.href=url;
};
$scope.submit = function(){
$scope.savedata();
//$scope.navigat('/success.html');
// $window.location.href='/success.html';
}
});
我还尝试调用提交函数,该函数调用其他两个函数 - savedata(( 和 navigat((- 但它只调用一个函数。
我还尝试将这两个函数放在两个不同的控制器中 - 然后在 body 元素上调用一个控制器,在按钮元素之前调用另一个在div 元素上调用另一个控制器。但看起来它甚至没有加载一个控制器。
请帮忙
你能试试这个吗:
app.controller('NavCtrl',function($scope,$location,$http,$window){
$scope.data = {};
$scope.savedata = function(){
$http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
.success(function(data,status,headers,config){
//console.log(data);
alert("successful");
$scope.navigat('/success.html');
})
.error(function(){
alert("failed");
})
};
$scope.navigat = function(url){
$window.location.href=url;
};
$scope.submit = function(){
$scope.savedata();
//$scope.navigat('/success.html');
// $window.location.href='/success.html';
}
});
您可以将 navigat('/success.html'( 放在 savedata 中发布调用的成功回调中
你应该在savedata
成功内部调用导航。
因为当您的savedata
被调用并等待 ajax 响应时,navigate
正在被调用并导航到新页面。
按如下方式使用回拨
$scope.savedata = function(callback){
$http.post("http://localhost/angular/insert2.php",{'place':$scope.data.place,'pincode':$scope.data.pincode})
.success(function(data,status,headers,config){
alert("successful");
if(callback) callback($scope);
})
.error(function(){
alert("failed");
})
};
调用如下
<button class="button button-block button-positive" ng-click="savedata(function (s) {s.navigat('/success.html')} );" >
Submit
</button>
http://plnkr.co/edit/QF80s7YxsLor70cNxLRk
相关文章:
- 无法在通过jQuery的ajax加载的页面中执行javascript
- JavaScript执行暂时挂起页面
- 如何在ReactJS JSX中执行嵌套的if-else语句
- 当js函数's已执行
- 哪个先执行
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- HTML表单提交时未执行外部函数
- 转义符不能与innerHTML一起使用
- 在Safari执行javascript之前对其进行修改
- Amd,希望确保某个东西总是最后执行
- 与 promise 一起使用时,异步瀑布不执行下一个回调方法
- Javascript不与ASP.Net中的ClientScriptManager一起执行
- 两个 ng 单击函数不会一起执行
- 执行'continue'和'break'语句与for一起工作..在里面
- ActiveX/COM对象正在执行OutputDebugString,以便与JavaScript中的IE一起使用
- 可以'不要一起执行
- 是将数据作为属性附加到延迟对象,还是将数据与延迟对象一起发送到执行解析的函数
- 如何将AngularJS的$resource调用存储在一个数组中,然后一起执行
- 应该将原型与自动执行的匿名函数一起使用
- Ajax没有与Jasmine一起执行