Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 当模型更改时,如何在md select上触发ng更改?_Javascript_Angularjs_Angularjs Directive_Angular Material - Fatal编程技术网

Javascript 当模型更改时,如何在md select上触发ng更改?

Javascript 当模型更改时,如何在md select上触发ng更改?,javascript,angularjs,angularjs-directive,angular-material,Javascript,Angularjs,Angularjs Directive,Angular Material,我正在使用md select,并且需要在值更改时触发某些代码(构建国家/州选择器)。当我通过控件更改值时,它可以正常工作,但是当模型从代码更改时,我还需要让控件正确反映值。我使用ng change来触发更改代码(需要ng change,因为用户无需单击键盘即可更改值)。问题是,当代码中的值发生更改时,不会触发事件。更复杂的是,md选择live in指令,允许我在多个地方使用该设置 这是我的指令模板: <md-input-container class="md-block">

我正在使用md select,并且需要在值更改时触发某些代码(构建国家/州选择器)。当我通过控件更改值时,它可以正常工作,但是当模型从代码更改时,我还需要让控件正确反映值。我使用ng change来触发更改代码(需要ng change,因为用户无需单击键盘即可更改值)。问题是,当代码中的值发生更改时,不会触发事件。更复杂的是,md选择live in指令,允许我在多个地方使用该设置

这是我的指令模板:

<md-input-container class="md-block">
    <label>Country</label>
    <md-select name="country" ng-model="countryState.countryModel" ng-change="countryState.onCountrySelected()">
        <md-option ng-repeat="country in countryState.countries" ng-value="country.itemId">
            {{ country.name | translate }}
        </md-option>
    </md-select>
</md-input-container>

<md-input-container class="md-block">
    <label>State</label>
    <md-select name="state" ng-model="countryState.stateModel" ng-disabled="countryState.countryModel == null">
        <md-option ng-repeat="state in countryState.states" ng-value="state.itemId">
            {{ state.name | translate }}
        </md-option>
    </md-select>
</md-input-container>

基本上,我想弄清楚是否有一种方法可以触发onCountrySelected,它将覆盖所有3个用例。

您可以使用$scope。$watch

$scope.$watch(
   function valueGetter(){
      return smth;
   },
   function onChange(newSmth, oldSmth){
   }
)

谢谢你,瓦莱里!实际上,我在ctrl.countryModel上试过一块手表,它不起作用,但把valueGetter函数放在里面起作用了。
<country-state-input country-model="app.location.countryId" state-model="app.location.stateId" input-form="locationsForm"></country-state-input>
[
    {
        "itemId": 1,
        "name": "CA",
        "states": [
            {
                "itemId": 1,
                "name": "CA_ON",
                "abbreviation": "ON"
            },
            {
                "itemId": 2,
                "name": "CA_QC",
                "abbreviation": "QC"
            }
        ]
    },
    {
        "itemId": 2,
        "name": "US",
        "states": [
            {
                "itemId": 14,
                "name": "US_AL",
                "abbreviation": "AL"
            },
            {
                "itemId": 15,
                "name": "US_AK",
                "abbreviation": "AK"
            }
        ]
    }
]
$scope.$watch(
   function valueGetter(){
      return smth;
   },
   function onChange(newSmth, oldSmth){
   }
)