2016-03-14 171 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