$ watch不会监听元素的变化

时间:2014-03-05 17:34:27

标签: angularjs angularjs-scope

我的控制器中有这个代码:

app.controller('NewSeller', function($scope, $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;
            }
    }

    $scope.logo = null;

    $scope.$watch('common_commonbundle_standard_address.country',
            function(val) {
                $http.get(Routing.generate('states') + val).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.";
                    }
                });
            });

    $scope.functionAddCompany = function() {
        var company = {
            "name": $('#addName').val(),
            "social": $('#addSocial').val(),
            "rif": $('#addRIF').val(),
            "address": $('#addAddress').val(),
            "phone": $('#addCode').val() + '-' + $('#addNumber').val(),
            "address2": $('#addAddress2').val(),
            "number": $('#addNo').val(),
            "street": $('#addStreet').val(),
            "block": $('#addBlock').val(),
            "state": $('#addState').val(),
            "city": $('#addCity').val(),
            "zone": $('#addZone').val(),
            "ref": $('#addRef').val()
        };

        scope = angular.element($(".seller-layout.new")).scope();
        scope.section = 'segundo-paso';
    };

});

为什么,无论何时我加载页面,都会进行以下调用:

  

获取http://dev.com/app_dev.php/common/get-statesundefined 404(不是   实测值)

$scope.$watch是否应该关注select#common_commonbundle_standard_address.country选项?有什么问题?

这是相关的HTML代码:

<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-pristine ng-invalid ng-invalid-required">
 ...
</select>

2 个答案:

答案 0 :(得分:2)

如果您正在深入观察某个对象,则可能需要指定true作为检查对象相等性的第三个参数,即

$scope.$watch('common_commonbundle_standard_address', function (newval) {
  // .. do your stuff
}, true);

其次,你在选择中放了一个ng-model="common_commonbundle_standard_address.country吗?

很明显,当你可以使用ng-model绑定时,为什么要用jquery东西乱丢你的控制器代码?它不仅仅是荒谬的,而且由jquery触发的事件将超出angular的生命周期,因此它们不会被包含在$digest个周期中。

答案 1 :(得分:0)

这是plunker,有一件事是在范围内定义对象,范围不会监视属性

http://plnkr.co/edit/t3rQ3fHE8iPDOGKqJPMW?p=preview