Javascript $scope.value仅在第二次单击后更改

Javascript $scope.value仅在第二次单击后更改,javascript,angularjs,angularjs-scope,Javascript,Angularjs,Angularjs Scope,这是我的html代码: <div ng-if="user.type == 'none'"> <button ng-click="selectUserType('user')"></button> </div> <div ng-if="user.type !== 'none'"> //here goes input etc </div> 问题是,在控制台中第一次单击后,我看到$scope.user获得“user”值,然后立即

这是我的html代码:

<div ng-if="user.type == 'none'"> <button ng-click="selectUserType('user')"></button> </div>
<div ng-if="user.type !== 'none'"> //here goes input etc </div>
问题是,在控制台中第一次单击后,我看到$scope.user获得“user”值,然后立即返回到“none”,但位置更改为我需要的位置$用户在第二次单击后再次获得所需的值,并隐藏有用的块并显示所需的内容

我怎样才能用另一种方式做到呢?我需要一次点击完成所有这些

当我删除
$location.path('/signup/'+type)
时,它可以工作,但当user.type更改时,我仍然需要更改location.path

如果你看到一些语法错误或什么的话,我的英语很抱歉。这不是我的母语,谢谢。

试试下面的代码(这不是最好的方法):

控制器内的代码(或链接功能)

我猜您的控制器初始化两次,因此请尝试在控制器外部定义$user变量,例如在全局范围内(也可以尝试在$rootScope中定义它),以便控制器重新初始化时不会更改$user变量。

尝试此代码(这不是最好的方法):

控制器内的代码(或链接功能)


我猜您的控制器初始化了两次,所以请尝试在控制器之外定义$user变量,例如在全局范围内(您也可以尝试在$rootScope中定义它),这样当控制器重新初始化时,它不会更改$user变量。

在某种程度上,Dmitry Loskutov回答我,我使用了以下方法: 只需检查控制器开头的location.path,然后设置类型:

    var locPath = $location.path();
    if (locPath == '/signup') {
        $scope.user = {type: 'none'};
    } else if (locPath == '/signup/user') {
        $scope.user = {type: 'user'};
    }

    $scope.selectUserType = function(type) {
        $location.path('/signup/' + type);
        $scope.user.type = type;
    };

希望,这对某人有帮助,因为我已经浪费了大约4个小时来获得这个简单的解决方案>在某种程度上,德米特里·洛斯库托夫回答我,我用了这个: 只需检查控制器开头的location.path,然后设置类型:

    var locPath = $location.path();
    if (locPath == '/signup') {
        $scope.user = {type: 'none'};
    } else if (locPath == '/signup/user') {
        $scope.user = {type: 'user'};
    }

    $scope.selectUserType = function(type) {
        $location.path('/signup/' + type);
        $scope.user.type = type;
    };

希望,这对某人有所帮助,因为我已经浪费了大约4个小时来获得这个简单的解决方案>\u您确定需要“”标记:您在更改
$scope.user.type
之前是否尝试过设置
$location.path
?而且你的按钮标签没有关闭。奇怪的事情导致了奇怪的行为;)<代码>对不起,有一些错误,现在它是正确的look@JanS当然,我尝试了相同的反应,如果两个指令都缺少结束语。您是否将代码复制/粘贴到您的帖子中或徒手键入?我猜你只是输入了它,否则你甚至可能看不到浏览器中的按钮。您确定这正是您正在使用的代码吗?您确定这里需要“”标记:在更改
$location.path
之前,您是否尝试过设置
$scope.user.type
?而且你的按钮标签没有关闭。奇怪的事情导致了奇怪的行为;)<代码>对不起,有一些错误,现在它是正确的look@JanS当然,我尝试了相同的反应,如果两个指令都缺少结束语。您是否将代码复制/粘贴到您的帖子中或徒手键入?我猜你只是输入了它,否则你甚至可能看不到浏览器中的按钮。您确定这就是您正在使用的代码吗?您是否尝试跳过
$location.path('/signup/'+type)?可能是重新输入控制器(或链接函数)并设置
$scope.user={type:'none'}再次?当我删除
$location.path('/signup/'+type)时,它会工作正常,但我仍然需要更改位置。路径我已经编辑了答案。尝试通过此值初始化
$scope.user
$scope.user | |{键入:“无”}同样的结果,不幸的是我已经编辑了答案:)试试看。但是这是一种糟糕的方法,不要一直使用它:)你是否尝试跳过
$location.path('/signup/'+type)?可能是重新输入控制器(或链接函数)并设置
$scope.user={type:'none'}再次?当我删除
$location.path('/signup/'+type)时,它会工作正常,但我仍然需要更改位置。路径我已经编辑了答案。尝试通过此值初始化
$scope.user
$scope.user | |{键入:“无”}同样的结果,不幸的是我已经编辑了答案:)试试看。但这是一种糟糕的方法,不要一直使用它:)
$scope.user = $user;
$scope.selectUserType = function(type) {
    $location.path('/signup/' + type);
    $user.type = type;
    $scope.user = angular.extend({}, $user);
};
    var locPath = $location.path();
    if (locPath == '/signup') {
        $scope.user = {type: 'none'};
    } else if (locPath == '/signup/user') {
        $scope.user = {type: 'user'};
    }

    $scope.selectUserType = function(type) {
        $location.path('/signup/' + type);
        $scope.user.type = type;
    };