简体   繁体   English

Ionic popup confirm不会在if语句中执行代码

[英]Ionic popup confirm is not executing code in an if statement

I have code behind a button that, once pressed, the button is meant to call a function to throw a pop up on screen. 我有一个按钮背后的代码,一旦按下,按钮就是调用一个函数来弹出一个弹出屏幕。 Either way if the user selects ok or dance, another function will be called. 无论哪种方式,如果用户选择确定或跳舞,将调用另一个功能。 The problem is neither of these two functions are being called. 问题是这两个函数都没有被调用。 Am I missing something very obvious? 我错过了很明显的东西吗?

$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();
               }
        });
  };

When working in javascript you need to understand scope. 在javascript中工作时,您需要了解范围。 This is best explained here... w3schools.com/js/js_scope.asp. 这里最好解释一下...... w3schools.com/js/js_scope.asp。

In brief: 简单来说:

$scope is predefined in a controller (angular) and as such is available anywhere in that one controller. $ scope是在控制器中预定义的(角度),因此可在该控制器的任何位置使用。 $rootScope is available in all controllers. $ rootScope适用于所有控制器。 Please see this example of scope: 请参阅范围示例:

.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

})

Assuming the code in ctrl1 has already executed the only defined variables accessible from other controllers would be $rootScope.a and $rootScope.b. 假设ctrl1中的代码已经执行,则可以从其他控制器访问的唯一定义的变量是$ rootScope.a和$ rootScope.b。 All others would be out of scope and as such undefined. 所有其他人都将超出范围,因此未定义。

This is an oversimplification but serves to explain $scope and $rootScope in angular to beginners. 这是一个过于简单化,但用于向初学者解释$ scope和$ rootScope。

A common mistake is to confuse the definition of variables and objects $scope and $rootScope with var x = y; 一个常见的错误是混淆变量和对象$scope$rootScope的定义与var x = y; Check this first ie users vs $scope.users, if this is ok then look at scope as the next test. 首先检查一下ie users vs $ scope.users,如果可以,那么将范围看作下一个测试。

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

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