简体   繁体   English

角度选择,具有动态值的ng-init

[英]Angular select, ng-init with dynamic value

I'm trying to get a select box working in Angular. 我想让一个选择框在Angular中工作。 The problem I'm experiencing is to do with ng-init and setting it's default value from an object which is created during runtime. 我遇到的问题是使用ng-init并从运行时期间创建的对象中设置其默认值。 Heres my code: 这是我的代码:

        <select 
            ng-model="settings.editing.panel.data.production_company"
            ng-change="settings.editing.panel.data.production_company = selectValue"
            ng-init="selectValue = settings.editing.panel.data.production_company"  
        >
            <option 
                ng-repeat="(key, value) in lists.production_companies" 
                value="{{key}}"
                ng-selected="{{selectValue}}" 
                >
                    {{value}}
                </option>
        </select>

"lists.production_companies" is a simple key-value array of names, populated during initial page render, updated by ajax. “ lists.production_companies”是名称的简单键值数组,由初始页面呈现期间填充,由ajax更新。

The object "settings.editing.panel.data" starts its life as NULL, but later is loaded with a correctly formatted object which contains the property "production_company". 对象“ settings.editing.panel.data”以NULL开头,但后来加载了包含属性“ production_company”的格式正确的对象。

I have found setting ng-init to something like "ng-init="selectValue = 3" works fine. Setting a $scope.test = 3, then setting "ng-init="selectValue = test" works fine too. 我发现将ng-init设置为“ ng-init =“ selectValue = 3”之类的东西可以正常工作。设置$ scope.test = 3,然后设置“ ng-init =“ selectValue = test”也可以。

However, my dynamic value does not work. 但是,我的动态值不起作用。 How can I use my dynamically created object to set the value of this select box during runtime with the set-up I have? 如何在运行时使用我拥有的设置使用动态创建的对象来设置此选择框的值?

You question confused me somehow. 您的问题使我感到困惑。 The following snippet is a working one, is that what you want? 以下代码段是working代码,这就是您想要的吗?

 'use strict'; angular.module('DemoApp', []); angular.module('DemoApp').controller('DemoCtrl', ['$scope', function($scope){ $scope.lists={ production_companies: { "0": "prod 1", "1":"prod_2" }, }; $scope.settings={ editing: { panel: { data: null } } }; $scope.setData=function(data){ $scope.settings.editing.panel.data={ production_company: data }; }; }]); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="DemoApp" ng-controller="DemoCtrl"> <select ng-model = "settings.editing.panel.data.production_company"> <option ng-repeat = "(key, value) in lists.production_companies" value = "{{key}}">{{value}}</option> </select> <div>You select: {{settings.editing.panel.data.production_company}}</div> <button ng-click="setData('0')">Set Data to Prod1</button> <button ng-click="setData('1')">Set Data to Prod2</button> </div> 

<select 
            ng-model="settings.editing.panel.data.production_company"
            ng-options = "option as option.keyName for option in list.production_companies"
        > <!--set keyName equal to your object's key-->

        </select>

Then in your controller 然后在您的控制器中

$scope.settings.editing.panel.data.production_company = list.production_companies[0] // Or which value you want to assign

In my circumstances I was able to change my backends data format to set an object like: 在我的情况下,我能够更改后端数据格式以设置一个对象,例如:

{"id": 1, "name":"prod comp 1"} 

and then change my select model accordingly. 然后相应地更改我的选择模型。 In hindsight I needed this for the ID anyway. 事后看来,无论如何我都需要ID。

<select 
    ng-model="settings.editing.panel.data.production_company"
>
    <option 
        ng-repeat="option in settings.lists.production_companies" 
        value="{{option.id}}"
        ng-selected="{{option.id}} == settings.editing.panel.data.production_company"
    >
        {{option.name}}
    </option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM