2016-03-14 122 views
1

还有就是我的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 = {type: 'none'}; 
$scope.selectUserType = function(type) { 
$location.path('/signup/'+type); 
$scope.user.type = type; 
}; 

的问题是,在控制台第一次点击后,我看到$ scope.user获得'用户'的价值,然后立即回到'无',但位置改变到我所需要的。 $ scope.user在第二次点击后再次获得所需的值,并隐藏有用的块并显示需要的内容。

我怎么能用另一种方式做到这一点?我需要通过点击来完成所有这些

它的工作原理,当我删除$location.path('/signup/'+type),但我仍然需要改变我的location.path时user.type改变

对不起,我的英语,如果你看到一些语法错误或什么。这不是我的本地人,谢谢。

+0

您确定您需要''标记:

+0

您是否在更改'$ location.path'之前尝试设置'$ scope.user.type'?你的按钮标签也不关闭。 )奇怪的事情已经引起陌生人的行为;)'

+0

对不起,有一些错误,现在它有正确的样子 – scDisorder

回答

0

,德米特里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个小时得到这个简单的解决方案> _ <

0

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

var $user = {type: 'none'}; // define OUTSIDE of controller(or link function) 

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

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

我想你的初始化你的情况两次控制器,所以尝试在控制器之外定义$ user变量,例如在全局范围内(也可以尝试在$ rootScope中定义它),所以当控制器重新启动它时不会变换$ user变量。

上的方式
+0

的代码仍然没有改变:(但是,谢谢 – scDisorder

+0

你试过跳过'$ location.path( '/注册/' +型);'?也许它重新启动你的控制器(或链接功能)并设置'$ scope.user = {type:'none'};'再次? –

+0

它工作时,我删除'$ location.path('/注册/'+类型);'通常,但我仍然需要改变location.path – scDisorder