search

Home  >  Q&A  >  body text

angular.js - Why doesn't ng-change trigger when angular assigns a value to select?

1. The HTML code is as follows:

<!-- 这个是街道 -->
<select name="jd" ng-model="wgy.jd" class="form-control input-sm" ng-options="jd.code as jd.label for jd in bmOfJd" ng-change="getSqBm(wgy.jd)">
    <option value="">--请选择--</option>
</select>
<!-- 这个是社区 -->
<select name="sq" ng-model="wgy.sq" class="form-control input-sm" ng-options="sq.code as sq.label for sq in bmOfSq">
   <option value="">--请选择--</option>
 </select>

2. The JS code is as follows:

//如果选择街道,去取出社区
$scope.getSqBm = function(jd){
        BmService.getXzqh(jd)
            .then(function(data){
                $scope.bmOfSq = data;
            });
    };

3. Question: Because this is common to the new page and the query page, when editing the page, I assign a value to the street wgy.jd, but this will not trigger the street ng-change. How can I trigger it? What about ng-change?

巴扎黑巴扎黑2864 days ago888

reply all(4)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-15 17:01:57

    Thank you for your answers. I have found a way
    That is to manually call the ng-change method getSqBm($scope.wgy.jd) after assigning a value to wgy.jd.
    This way it will trigger the ng-change of the street! ! !
    Here are detailed instructions

    reply
    0
  • 怪我咯

    怪我咯2017-05-15 17:01:57

    It’s better to use watch to do it. Use changes in observed data to trigger a certain method

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-15 17:01:57

    http://stackoverflow.com/questions/14386570/getting-the-ng-object-selected-with-ng-change

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-05-15 17:01:57

    HTML

    <select ng-model="wgy.jd"
            ng-change="getSqBm()">
        <option value="">--请选择--</option>
        <option ng-repeat="jd in jds" value="{{jd.id}}">{{jd.name}}</option>
    </select>
    <select ng-model="wgy.sq">
        <option value="">--请选择--</option>
        <option ng-repeat="sq in sqs" value="{{sq.id}}">{{sq.name}}</option>
    </select>

    SCRIPT

    angular.module('myApp', [])
            .controller('MyCtrl', function ($scope) {
                $scope.wgy = {
                    jd: '',
                    sq: ''
                }
    
                $scope.jds = [
                    {
                        id: 1,
                        name: '街道1',
                        sq: [
                            {
                                id: 1,
                                name: '街道1-社区1'
                            },
                            {
                                id: 2,
                                name: '街道1-社区2'
                            }
                        ]
                    },
                    {
                        id: 2,
                        name: '街道2',
                        sq: [
                            {
                                id: 1,
                                name: '街道2-社区1'
                            },
                            {
                                id: 2,
                                name: '街道2-社区2'
                            }
                        ]
                    }
                ];
    
                $scope.sqs = [];
    
                $scope.getSqBm = function(){
                    if(!$scope.wgy.jd){
                        $scope.sqs = [];
                        return ;
                    }
    
                    $scope.sqs = $scope.jds.filter(function(jd){
                        return jd.id == $scope.wgy.jd;
                    })[0].sq;
                }
            });

    reply
    0
  • Cancelreply