2014-03-05 196 views
0

我有這樣的HTML:訪問UI元素

<select id="common_commonbundle_standard_address_country" 
     ng-model="common_commonbundle_standard_address.country" 
     required="required" 
     tooltip="País" 
     tooltip-trigger="focus" 
     tooltip-placement="right" 
     wv-def="País" 
     wv-cur="" 
     wv-err="Error!" 
     wv-req="The value you selected is not a valid choice" 
     type="text" 
     class="ng-scope ng-dirty ng-valid ng-valid-required"> 
    ... 
</select> 

而且我使用此代碼試圖common_commonbundle_standard_address.country要監視更改:

$scope.$watch('$scope.common_commonbundle_standard_address.country', function(iso_country) { 
    if (iso_country) 
     $http.get(Routing.generate('states') + iso_country).success(function(data) { 
      if (data.message) { 
       $scope.message = data.message; 
      } else { 
       $scope.states = data; 
      } 
     }).error(function(data, status, headers, config) { 
      if (status == '500') { 
       $scope.message = "No hay conexión con el servidor."; 
      } 
     }); 
}); 

但它不工作,有什麼我做錯了?

新增控制器

app.controller( 'NewSeller',函數($範圍,$ HTTP,$ routeParams,$ fileUploader){

$scope.section = $routeParams.section == undefined ? 'main' : $routeParams.section; 
$scope.aaaa = 'sdsdsdsd'; 

switch ($scope.section) { 
    case 'registro': 
     { 
      $('.seller-menu').animate({left: -230}, 300); 
      $scope.section = 'primer-paso'; 
      break; 
     } 
    case 'segundo-paso': 
     { 
      break; 
     } 
    case 'main': 
    default: 
     { 
      break; 
     } 
} 

// Add watch for states 
$scope.$watch('common_commonbundle_standard_address.country', 
     function(iso_country) { 
      $http.get(Routing.generate('states') + iso_country).success(function(data) { 
       if (data.message) { 
        $scope.message = data.message; 
       } else { 
        $scope.states = data; 
       } 
      }).error(function(data, status, headers, config) { 
       if (status == '500') { 
        $scope.message = "No hay conexión con el servidor."; 
       } 
      }); 
     }, true); 

});

+0

您是否使用了調試器來查看您的處理程序是否被調用或者處理程序中是否有錯誤? –

+2

'$ scope.common_commonbundle_standard_address.country'應該是'common_commonbundle_standard_address.country' – Whisher

回答

1

您不需要在您的手錶表達中包含。您可能還想嘗試使用對象相等性:

$scope.$watch('common_commonbundle_standard_address.country', 
    function(country) { 
     // Do some work here 
    }, true); 
+0

也不能這樣工作 – ReynierPM

+0

@ReynierPM - 我對可能有幫助的答案做了一些小改動。 –

+0

它的工作原理,但是如果我在'common_commonbundle_standard_address.country'處沒有改變任何東西,那麼每次加載頁面eveb時都會執行該函數,爲什麼? – ReynierPM