AngularJS:选择NG-Options+ Div ng-repeat中的1个选项don' t选择相同的默认值

发布于 2025-01-29 13:21:04 字数 2417 浏览 4 评论 0原文

我没有找到一些可以帮助我有关该主题的答案的代码。 我在angularjs中的级别是0,所以我也不理解代码^^

不是我的代码:我必须调试/纠正它: 我有一个NG重复和一个按钮,可以添加我想要的多次DIV。 默认选择的初始DIV第一个“ ACT”和加法DIV具有首选 < option value =“”> select</option>选择,这就是我想要的。

<div ng-repeat="item in vm.ListActs track by $index">
    <fieldset>
        <legend>Act {{$index + 1}}<i class="icon-close" ng-click="vm.DeleteAct($index)" ng-hide="$index === 0"></i></legend>
        <br />
        <div class="field selectField">
            <label for="selectgroupAct{{$index}}">
                TO <span class="obligatoire">*</span>
            </label>
            <select id="selectgroupAct{{$index}}"
                    name="selectgroupAct{{$index}}"
                    ng-options="act.Code as acte.Code for acte in vm.ListActsTO.Acts"
                    ng-disabled="vm.ListActsTO.Acts.length === 1"
                    ng-model="item.Code"
                    ng-change="vm.ActIndexChange($index, item.Code)"
                    required >
                    <option value="">Select</option>
            </select>
        </div>
    </fieldset>
</div>

您知道为什么初始选择不想将&lt; option value =“”&gt; select&lt;/option&gt;作为所选选项?

在JS中,我有此代码:

(function () {
    'use strict';
    angular
        .module('SpaApp')
        .controller('myController', myController);

    myController.$inject = ['firstService', 'secondService', '$localStorage', '$location'];

    function myController(firstService, secondService, $localStorage, $location) {
        var $ctrl = this;

        angular.extend($ctrl, {
            ...methods declared
        });

        init();
        return $ctrl;


        function init() {
            angular.extend($ctrl, {
                ListActsTO: {},
                ListActs : []
            });

            getListActsTO();
            $ctrl.ListActs [0] = {
                Code: ""
            };
        }
        ...

我尝试在我的选项&lt; option value =“”&gt; select&lt;/option&gt;中添加选择的属性。我尝试了 https://docs.angularjs.orgs.org/api/api/api/ng/ng/ng/directive/select/select/select 但是我还不够好,在应用一个,我不再工作的其他机构。

感谢您的帮助

I didn't found some code that can help me among all answers about the subject.
My level in AngularJS is 0 so I don't understand the code too ^^

It's not my code : I have to debug/correct it :
I have a ng-repeat and a button to add a div as many times I want.
The initial div selected by default the first "Act" and the additionnal div has the first choice
<option value="">Select</option> selected and that's what I want.

<div ng-repeat="item in vm.ListActs track by $index">
    <fieldset>
        <legend>Act {{$index + 1}}<i class="icon-close" ng-click="vm.DeleteAct($index)" ng-hide="$index === 0"></i></legend>
        <br />
        <div class="field selectField">
            <label for="selectgroupAct{{$index}}">
                TO <span class="obligatoire">*</span>
            </label>
            <select id="selectgroupAct{{$index}}"
                    name="selectgroupAct{{$index}}"
                    ng-options="act.Code as acte.Code for acte in vm.ListActsTO.Acts"
                    ng-disabled="vm.ListActsTO.Acts.length === 1"
                    ng-model="item.Code"
                    ng-change="vm.ActIndexChange($index, item.Code)"
                    required >
                    <option value="">Select</option>
            </select>
        </div>
    </fieldset>
</div>

Do you know why the initial select doesn't want to have the <option value="">Select</option> as the selected option ?

In the js I have this code:

(function () {
    'use strict';
    angular
        .module('SpaApp')
        .controller('myController', myController);

    myController.$inject = ['firstService', 'secondService', '$localStorage', '$location'];

    function myController(firstService, secondService, $localStorage, $location) {
        var $ctrl = this;

        angular.extend($ctrl, {
            ...methods declared
        });

        init();
        return $ctrl;


        function init() {
            angular.extend($ctrl, {
                ListActsTO: {},
                ListActs : []
            });

            getListActsTO();
            $ctrl.ListActs [0] = {
                Code: ""
            };
        }
        ...

I tried to add property selected in my option <option value="">Select</option> but it didn't work. I tried with https://docs.angularjs.org/api/ng/directive/select but I'm not good enough and after applying one, the other functionnalities I have didn't work anymore.

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-02-05 13:21:04

ng模型是这里的关键。它说'item.Code是在此表单元素中跟踪的变量。

 <select id="selectgroupAct{{$index}}"
 ...
 ng-model="item.Code"
 ...
 required >

也就是说,在控制器中,您可以简单地初始化此变量以匹配该值,在这种情况下为空字符串。

  function myController(firstService, secondService, $localStorage, $location) {
    var $ctrl = this;
    $ctrl.item = {Code: ""}
    .....

(前提

The ng-model is the key here. It says 'item.Code is the variable being tracked in this form element'.

 <select id="selectgroupAct{{$index}}"
 ...
 ng-model="item.Code"
 ...
 required >

That said, in your controller, you can simply initialize this variable to match the value, in this case an empty string.

  function myController(firstService, secondService, $localStorage, $location) {
    var $ctrl = this;
    $ctrl.item = {Code: ""}
    .....

(assuming you don't have other empty string values in your vm.ListActsTO.Acts array)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文