AngularJS ng-change on select not triggering

时间:2017-03-07 20:28:35

标签: php angularjs

我是AngularJS的新手,我每天都在学习。但是连续两天我都遇到了这个ng-change问​​题。我有2个选择输入。第二输入具有基于第一输入选项的值。我做过这样的例子,但现在它不起作用。我不知道我是否有一个combinemodule或者是否来自ng-selected。这是我的代码:

<div class="col-lg-4">
            <section class="panel">
                <div class="panel-body">
                    <div class="panel-heading">Adresa teren</div>
                    <form class="form-horizontal" data-validate="parsley"
                          action="forms.php?forms=update_land_address" method="post" id="form_land_address">

                        <div class="form-group">
                            <label class="col-sm-3 control-label">Judet</label>

                            <div class="col-sm-9">
                                <input type="hidden" id="land_address_judet_id"
                                       value="<?php echo $land_address_data[0]['id_judet']; ?>">
                                <div ng-app="land_address_judet_module" ng-controller="land_address_judet_controller">
                                <select id="land_address_judet" class="form-control" name="land_address_judet"  ng-change="ChangeJudet()" ng-model="uat_superior_module">
                                    <option ng-repeat="h in rez" value="h.id_judet"
                                            ng-value="h.id_judet"  ng-selected="h.id_judet=='<?php echo $land_address_data[0]['id_judet']; ?>'"  >
                                        {{h.judet}}
                                    </option>
                                </select>
                                </div>

                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-3 control-label">UAT</label>

                            <div class="col-sm-9" ng-controller="uat_superior" ng-app="uat_superior_module">
                                <select id="land_address_uat" class="form-control" name="land_address_uat">

                                    <option ng-repeat="h in rez"
                                            value="h.sirsup" <?php if ("h.sirsup" == $land_address_data[0]['sirsup']) {
                                        echo "selected=\"selected\"";
                                    } ?> ng-selected="h.sirsup==<?php echo $land_address_data[0]['sirsup']; ?>">
                                        {{h.localitate_superioara}}
                                    </option>


                                </select>
                            </div>


                    </form>
                </div>
            </section>
        </div>

这是角度脚本的一部分:

var tip_judet = 'get_judet';
var address_judet = "cautare.php?tip=get_judet";
var app_judet = angular.module("land_address_judet_module", []);
app_judet.controller('land_address_judet_controller', function ($scope, $http) {
    $http.get(address_judet)
        .then(function (response) {
            $scope.rez = response.data.records;
        });
});

var tip3 = 'get_uat_superior';
var judet = $('#land_address_judet_id').val();

var address3 = "cautare.php?tip=" + tip3 + "&id_judet=" + judet;
var app3 = angular.module("uat_superior_module", []);
app3.controller('uat_superior', function ($scope, $http) {
    $http.get(address3)
        .then(function (response) {
            $scope.rez = response.data.records;
        });
            $scope.ChangeJudet = function () {
                alert('da');
                judet = $('#land_address_judet').val();
                var address = "cautare.php?tip=" + tip3 + "&id_judet=" + judet;
                $http.get(address)
                    .then(function (response) {
                        $scope.rez = response.data.records;
                    });
            }


});


angular.module("CombineModule", ["formular_land", "formular_land_address", "uat_superior_module", "land_address_judet_module"]);

从上面的代码中,第一个输入是空白的,当我更改数据时,它不会触发$ scope.ChangeJudet

谢谢

2 个答案:

答案 0 :(得分:1)

您的$scope.ChangeJudet功能位于控制器uat_superior中,并且您在使用land_address_judet_controller时尝试访问它。您可以尝试使用$rootScope,因为它可用于整个应用程序,或者只使用适当的控制器。

答案 1 :(得分:0)

不要使用多个project.evaluationDependsOnChildren()指令。

ng-app
  

使用<!-- ERRONEOUS --> <div ng-app="land_address_judet_module" ng-controller="land_address_judet_controller"> <div class="col-sm-9" ng-controller="uat_superior" ng-app="uat_superior_module"> 时需要注意以下事项:

     
      
  • 每个HTML文档只能自动引导一个AngularJS应用程序。文档中找到的第一个ng-app将用于定义作为应用程序自动引导的根元素。
  •   
     

— AngularJS ng-app Directive API Reference