[英]Ionic popup confirm is not executing code in an if statement
我有一个按钮背后的代码,一旦按下,按钮就是调用一个函数来弹出一个弹出屏幕。 无论哪种方式,如果用户选择确定或跳舞,将调用另一个功能。 问题是这两个函数都没有被调用。 我错过了很明显的东西吗?
$scope.user = {};
// A confirm dialog
$scope.showConfirm = function() {
var confirmPopup = $ionicPopup.confirm({
title: 'T&Cs',
template: 'Do you agree to the terms and conditions?'
});
confirmPopup.then(function(res) {
if( res ) {
signIn(user)
} else {
logout();
}
});
};
在javascript中工作时,您需要了解范围。 这里最好解释一下...... w3schools.com/js/js_scope.asp。
简单来说:
$ scope是在控制器中预定义的(角度),因此可在该控制器的任何位置使用。 $ rootScope适用于所有控制器。 请参阅范围示例:
.controller('ctrl1', function($scope, $rootScope) {
var a = 1;
$scope.a = 2;
$rootScope.a = 3;
var funct1 = function () {
var b = 4;
$scope.b = 5;
$rootScope.b = 6;
console.log(a) // returns 1 as inherits scope
console.log($scope.a) // returns 2 as inherits $scope scope
console.log($rootScope.a) // returns 3 as inherits $rootScope scope
console.log(b) // returns 4 as shares scope
console.log($scope.b) // returns 5 as inherits $scope scope
console.log($rootScope.b) // returns 6 as inherits $rootScope scope
}
funct1();
console.log(a); // returns 1 as shares scope
console.log($scope.a); // returns 2 as inherits $scope scope
console.log($rootScope.a); // returns 3 as inherits $rootScope scope
console.log(b); // returns undefined as b is defined out of scope.
console.log($scope.b); // returns 5 as inherits $scope scope
console.log($rootScope.b); // returns 6 as inherits $rootScope scope
})
假设ctrl1中的代码已经执行,则可以从其他控制器访问的唯一定义的变量是$ rootScope.a和$ rootScope.b。 所有其他人都将超出范围,因此未定义。
这是一个过于简单化,但用于向初学者解释$ scope和$ rootScope。
一个常见的错误是混淆变量和对象$scope
和$rootScope
的定义与var x = y;
首先检查一下ie users vs $ scope.users,如果可以,那么将范围看作下一个测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.