为什么ng-change在angularjs中给出“未定义”?

时间:2018-11-06 07:34:07

标签: javascript angularjs

您能否告诉我为什么$id = $request->route('id'); //laravel 5.7 print_r($id); //just want to see the id here exit(); 在angularjs中给出“未定义”?

ng-change

这是我的代码 https://plnkr.co/edit/HLLUcVnIl8ySg8baSMHv?p=preview

5 个答案:

答案 0 :(得分:1)

您可以将ng-model值直接传递给函数,

 <select class="form-control" ng-model="a" ng-options="item.city as item.city for item in vals" ng-change="onChangedCityDropDown(a)">
    <option value="" disabled>choose an option</option>
 </select>

答案 1 :(得分:1)

ng-options的工作方式与ng-repeat的工作方式不同。除了item属性值之外,您不会获得ng-options值。要使其正常工作,您应该在ng-model回调函数中使用ng-change(a)值。

ng-change="onChangedCityDropDown(a)"

您还想根据城市选择设置state,然后将ng-options更改为以下

ng-options="item.state as item.city for item in vals"

Demo Plunker

答案 2 :(得分:1)

只需将onChangedCityDropDown(item)更改为onChangedCityDropDown(a)

see

答案 3 :(得分:0)

只需更改ng-options如下所示,即可为您提供期望的对象。

ans

ng-options="item as item.city for item in vals" ng-change="onChangedCityDropDown(a)" 中,您必须通过ng-change

这是工作中的example

答案 4 :(得分:0)

您可以尝试像下面的代码那样选择城市和州参考,还请检查给定工作示例的this plunker link

控制器:

$scope.vals = [{
    "city":"gurgaon",
    "state":"Haryana"
  }, {
    "city":"Manesar",
    "state":"Haryana"
  }, {
    "city":"Bangalore",
    "state":"Karnataka"
  }]; 

  $scope.onChangedCityDropDown= function(i){
    console.log(i)
  }

  $scope.onChangedStateDropDown= function(i){
    console.log(i)
  }

模板:

City
<select  class="form-control" ng-model="city" ng-options="item.city as item.city for item in vals" ng-change="onChangedCityDropDown(city)">
    <option value="" disabled>Select City</option>
</select>
State
<select  class="form-control" ng-model="state" ng-options="item.state as item.state for item in vals | filter:{city:city}" ng-change="onChangedStateDropDown(state)">
    <option value="" disabled>Select State</option>
</select>