2015-04-02 61 views
0

我試圖在角度控制器中使用它來獲取$ scope.selectedIndicator方法以外的方法。在angularJs控制器中使用jquery方法中的變量

關於如何獲得$ scope.name的任何想法?上述小提琴的評論也是受歡迎的。

$scope.setSelectedIndicator = function() { 
      $('#indicatorSelect').on('change', function(){ 
       $scope.selectedIndicator=$('#indicatorSelect').chosen().val(); 
       var res = $scope.selectedIndicator.split("|"); 
       $scope.selectedIndicatorId=res[0]; 
       $scope.selectedIndicatorName=res[1]; 
       console.log($scope.selectedIndicatorName); 

       }); 
      $scope.name = $scope.selectedIndicatorName ; 
     }; 
     $scope.setSelectedIndicator(); 
+0

所以你嘗試做什麼:在變更事件或其他設置上設置'$ scope.name'? – Grundy 2015-04-02 10:48:42

回答

0

首先,你可以使用角度的ng-change指令用於這一目的。最好避免jQuery內部的角度,也只是爲了得到從indicatorSelect選擇的值,jQuery不是必需的。你可以通過angular.element自己做到。

反正,在您的jQuery change事件處理程序中,您必須使用$scope.$apply()來使selectedIndicatorName中的更改反映在$scope.name中。

由於您處於角度環境之外,因此需要運行$scope.$apply()以便更改反映。

+0

問題用$ scope解決。$ apply()thx;) – kenza 2015-04-02 13:34:09

+0

很好聽。但爲什麼我不接受;) – mohamedrias 2015-04-02 14:07:45

0

$適用於angularJS。您將不得不使用jQuery而不是$。這是你的新功能:

 jQuery('#indicatorSelect').on('change', function(){ 
      $scope.selectedIndicator = jQuery('#indicatorSelect').chosen().val(); 
      var res = $scope.selectedIndicator.split("|"); 
      $scope.selectedIndicatorId=res[0]; 
      $scope.selectedIndicatorName=res[1]; 
      console.log($scope.selectedIndicatorName); 

      }); 
+0

$用於$ http,$ filter,$ q等所有的angularjs服務中。除此之外'$'沒有按角度分配給任何函數。但是最好使用jQuery來避免衝突。 – mohamedrias 2015-04-02 17:09:58

0
$scope.setSelectedIndicator = function() { 
      $scope.selectedIndicatorName=''; 
      jQuery('#indicatorSelect').on('change', function(){ 
       $scope.selectedIndicator = jQuery('#indicatorSelect').chosen().val(); 
       var res = $scope.selectedIndicator.split("|"); 
       $scope.Id=res[0]; 
       $scope.Name=res[1]; 
       $scope.$apply(function() { 
        $scope.selectedIndicatorName = $scope.Name; 
       }); 
       console.log($scope.selectedIndicatorName); 

      }); 
相關問題