根据字符串AJAX值设置ng-options值

时间:2016-12-14 10:44:17

标签: angularjs select angular-ngmodel ng-options

我有两个选择菜单,通过页面加载后获取的AJAX JSON数据填充。它们通过自己的模型链接,看起来像这样;

<select name="category" 
        id="category" 
        ng-model="category" 
        ng-options="category as category.name for category in categories track by category.name">
    <option value=''>Select category</option>
</select>

<select name="product" 
        id="product" 
        ng-disabled="!category" 
        ng-model="product" 
        ng-options="product for product in category.products">
    <option value=''>Select product</option>
</select>

将表单提交给API后,如果使用origincategory字符串,则会返回product个对象。

我认为我应该能够将$scope.category和/或$scope.product设置为返回的origin数据,以便在选择菜单中预先选择项目;

.success(function(data)) { 
...
$scope.category = data.origin.category
...   
}

但这不起作用......

2 个答案:

答案 0 :(得分:1)

您可以使用ng-init选择初始值

<select ng-init="initDefaultCategory(categories)" name="category" id="category" ng-model="category" ng-options="category as category.name for category in categories track by category.name">
    <option value=''>Select category</option>
  </select>

  <select ng-init="initDefaultProduct(category.products)" name="product" id="product" ng-disabled="!category" ng-model="product" ng-options="product for product in category.products">
    <option value=''>Select product</option>
  </select>

正在使用 DEMO

<强> EDITS

如果要选择默认值,可以执行以下操作

<强>控制器

$scope.initDefaultCategory = function(categories, value) {
    if (categories.length > 0)
      $scope.category = categories.filter(e => e.name == value)[0];
  }

您可以在ng-init

中传递值
  <select ng-init="initDefaultCategory(categories,'All')" name="category" id="category" ng-model="category" ng-options="category as category.name for category in categories track by category.name">
    <option value=''>Select category</option>
  </select>

Edited Demo

答案 1 :(得分:1)

public class MonthAxisValueFormatter implements IAxisValueFormatter {

    protected String[] mMonths;
    private Context context;

    private BarLineChartBase<?> chart;

    public MonthAxisValueFormatter(BarLineChartBase<?> chart, Context context) {
        this.chart = chart;
        this.context = context;

        mMonths = new String[]{
                this.context.getResources().getString(R.string.january),
                this.context.getResources().getString(R.string.february),
                this.context.getResources().getString(R.string.march),
                this.context.getResources().getString(R.string.april),
                this.context.getResources().getString(R.string.may),
                this.context.getResources().getString(R.string.june),
                this.context.getResources().getString(R.string.july),
                this.context.getResources().getString(R.string.august),
                this.context.getResources().getString(R.string.september),
                this.context.getResources().getString(R.string.october),
                this.context.getResources().getString(R.string.november),
                this.context.getResources().getString(R.string.december)
        };
    }

    @Override
    public String getFormattedValue(float value, AxisBase axis) {

        int month = (int) value;

        String monthName = mMonths[month % mMonths.length];

        return monthName;
    }
}