Angularjs 设置所选ng模型的默认值并管理ng更改

Angularjs 设置所选ng模型的默认值并管理ng更改,angularjs,angular-ngmodel,angularjs-ng-change,Angularjs,Angular Ngmodel,Angularjs Ng Change,我无法设置所选项目的默认值,因为我知道应该从json()获取默认值。请使用该选定值显示另一个表单 在我的控制器中,我已通过以下方式设置所选参数: .controller('attributeFacetCtrl', function ($scope, tabsService, $location, contentService, attribute) { $scope.attribute = attribute; $scope.types = [{ val: '

我无法设置所选项目的默认值,因为我知道应该从json()获取默认值。请使用该选定值显示另一个表单

在我的控制器中,我已通过以下方式设置所选参数:

.controller('attributeFacetCtrl', function ($scope, tabsService, $location, contentService, attribute) {

    $scope.attribute = attribute;

    $scope.types = [{
        val: 'terms'
    }, {
        val: 'continuous'
    }];

    $scope.selected =  $scope.attribute.facet.data.type;

    $scope.isTerms = false;

    $scope.validateFrom = function() {

        var chk = $scope.selected.val;

        if (chk === 'terms') {
            $scope.isTerms = true;
        } else {
            $scope.isTerms = false;
        }
    };
})
$scope.attribute.facet.data.type=“terms”的结果

在我看来,我有这个

 <div class="form-group">
                        <label class="wk-field-label">
                            <i class="icon-asterisk wk-prefield wk-mandatory"></i>
                            Type
                        </span>
                        </label>
                        <select class="wk-field-input" ng-model="selected"
                                ng-options="typeValue as typeValue.val for typeValue in types"
                                ng-change="validateFrom()"
                                required></select>
                    </div>
                    <div ng-if="isTerms" ng-repeat="orders in attribute.facet.data.order">
                        <div>
                            <label class="wk-field-label">
                                <i class="icon-asterisk wk-prefield wk-mandatory"></i>
                                Order
                                </span>
                            </label>
                    </div>
 </div>

类型
命令

问题在于,您正在将
$scope.selected
设置为字符串(“术语”),但在加载
ng options
时,加载的对象具有
val
属性

如果不一定需要选择
$scope.selected
作为对象,则可以将
ng选项更改为以下内容:

ng-options = "typeValue.val as typeValue.val for typeValue in types"
<select 
  class="wk-field-input" 
  ng-model="selected"
  ng-options="typeValue as typeValue.val for typeValue in types"
  ng-change="validateFrom()"
  required
>
</select>

如果需要将
$scope.selected
作为具有
val
属性的对象,则需要适当地设置其初始值,而不是将其设置为字符串,以便设置默认选择。

由于要按字符串设置元素,请将
select
元素更改为以下内容:

ng-options = "typeValue.val as typeValue.val for typeValue in types"
<select 
  class="wk-field-input" 
  ng-model="selected"
  ng-options="typeValue as typeValue.val for typeValue in types"
  ng-change="validateFrom()"
  required
>
</select>

您可以在此处阅读有关如何将
用作
的更多信息:


希望有帮助

console.log($scope.attribute.facet.data.type)的输出是什么
?console.log outpus:terms我更新了我的问题的typeValue,因为它没有改变,默认的选择值在我的viewHard code
$scope上仍然为空。选择
作为
{val:'terms'}
,然后尝试将其编码为
terms
-这两个选项中的一个必须工作,但我肯定这是我最初建议的方式。硬编码至少可以消除其中一个:)在my ng选项中使用typeValue.val是正确的