2016-01-20 67 views
-1

我有這樣的代碼數據未保存在變量中?

$scope.setContent = function (data) { 
     if ($scope.unfiltered_tree == null) { 
      $scope.unfiltered_tree = data; 
     } 
    } 

保存在unfiltered_tree首次數據,而當新數據來自它不會被保存。

對此有何幫助?

+0

你有控制檯登錄的內部,如果子句來驗證它是否被觸發? –

+0

未保存,因爲它不爲空。由於它不爲空,所以'if'語句失敗並且不運行 –

+2

刪除'if'語句,因爲只有在'null'時纔會設置'$ scope.unfiltered_tree'。 – akmozo

回答

0

我不完全理解你的問題。你問如何始終將新數據存儲在變量中?只要刪除if語句。

$scope.setContent = function (data) { 
     $scope.unfiltered_tree = data; 
    } 

if語句的目的可能是爲了檢查你的變量已經intialized與否。在這種情況下,你可能需要使用以下格式:

$scope.setContent = function (data) { 
     if (!$scope.unfiltered_tree) { 
      // Variable has not been initialized yet 
      $scope.unfiltered_tree = data; 
     } else { 
      // Variable has been initialized before, do something else 
      } 
    }