2014-03-05 153 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