2016-01-23 25 views
0

我有我的應用程序反饋功能,目標NG-如果外控制器

#feedback 
    %h3.button 
    %a{"ui-sref" => ".feedback", "ng-click" => "feedbackActive=!feedbackActive;"} 
     Feedback 
    #feedback-container{"ng-if" => "feedbackActive"} 
    %div{"ui-view" => "feedback"} 

#feedback-container具有ng-if,以便在需要時,內容才加載。 %a有一個ng鍵,在feedbackActive狀態之間切換true/false。

這工作正常。無論如何,在我的ui-view中,我加載了一個模板。在該模板發送按鈕具有send()功能鏈接到feedbackCtrl

$scope.send = function(){ 
    console.log ('send') 
    console.log ($scope.feedbackForm) 

    createFeedback.create({ 
    name:  $scope.feedbackForm.name, 
    feedback: $scope.feedbackForm.feedback 
    }) 
    $scope.feedbackActive = false; 
} 

它運行的代碼罰款,但這樣沒有任何反應不給feedbackActive假值。

如何從控制器外部切換ng-if

+0

開始使用controllerAs它應該唯一的這個問題。 – dfsq

+0

你能否提供適當的條件代碼? – Viplock

+0

@Viplock你是什麼意思?這是我用於反饋功能的所有代碼。 – alucardu

回答

1

這是一個經典的範圍界定問題。您的管制員的範圍是ng-if指令範圍內的子女。一種選擇是使用$scope.$parent在父範圍上設置變量。

$scope.send = function(){ 
    console.log ('send') 
    console.log ($scope.feedbackForm) 

    createFeedback.create({ 
    name:  $scope.feedbackForm.name, 
    feedback: $scope.feedbackForm.feedback 
    }) 
    $scope.$parent.feedbackActive = false; 
} 

另一種選擇是利用原型繼承。在父控制器中,初始化一個對象。

$scope.x = {}; //parent scope 

在視圖控制器中,設置繼承對象的屬性。

$scope.x.feedbackActive = false; //child scope 

,當然還有你的HTML

<feedback-container ng-if="x.feedbackActive">