select标签上的值HTML将数组转换为字符串Javascript

时间:2016-01-24 03:16:29

标签: javascript html arrays angularjs angular-ngmodel

我在Angular Js中使用Javascript,在我的控制器中我有以下项目:

$scope.address = [];
$scope.states = [
    { name:"State 1", cities:[ "City Mexico 1", "City Mexico 2","City Mexico 3"] },
    { name:"State 2", cities:["City  1", "City  2", "City  3"] }
  ];

在我看来,我在做:

<select ng-model="address.state" placeholder="Estado" >
            <option ng-repeat="state in states" value="{{state}}"> {{state.name}}</option>
          </select>  

如果我选择一个选项,然后我执行console.log(address.state),我将对象作为String而不是数组。如何将对象作为数组(json)。

控制台输出如下所示:

[state: "{"name":"State 1","cities":["City Mexico 1","City Mexico 2","City Mexico 3"]}"]

我想把对象作为数组,因为那时我做了$ Scope.address.state.city

1 个答案:

答案 0 :(得分:0)

您可以在ng-repeat代码中<option>代替ng-options标记上的<select>

<select ng-model="address.state" ng-options="state.name for state in states" placeholder="Estados"></select>

这样,状态对象就会传递到$scope.address,你可以$scope.address.state.cities

工作示例:https://plnkr.co/edit/k2gK5GNEt2xTrMbwih7V?p=preview