为什么Angular默认情况下没有选择正确的选择选项?

时间:2017-04-25 16:37:10

标签: angularjs

我们有一个像这样的Select标签:

<select id="nonemployeeSelect"
        ng-model="data.nonEmployeeCase.nonEmployeeId">
    <option ng-repeat="nonemployee in data.nonemployees"
            ng-value={{nonemployee.id}} ng-selected="{{nonemployee.id == @ViewBag.nonempId}}">
        {{nonemployee.organization.displayValue}}
    </option>
</select>

ng-model绑定到Angular控制器中初始化的属性,如下所示:

$scope.data.nonEmployeeCase = {};
$scope.data.nonEmployeeCase.nonEmployeeId = 1;

ng-repeat来自范围内的非雇员数组:

$scope.data.nonemployees

所以选择的html最终看起来像这样:

 <select id="nonemployeeSelect" ng-model="data.nonEmployeeCase.nonEmployeeId" class="ng-pristine ng-untouched ng-valid ng-not-empty">
        <!-- ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="1" ng-selected="false" class="ng-binding ng-scope" value="number:1" selected="selected">
            Office of the Administrator
        </option><!-- end ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="2" ng-selected="false" class="ng-binding ng-scope" value="number:2">
            Office of Women’s Health
        </option><!-- end ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="3" ng-selected="true" class="ng-binding ng-scope" value="number:3">
            Office of Budget
        </option><!-- end ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="4" ng-selected="false" class="ng-binding ng-scope" value="number:4">
            Office of the Administrator
        </option><!-- end ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="5" ng-selected="false" class="ng-binding ng-scope" value="number:5">
            Division of Budget Execution and Management
        </option><!-- end ngRepeat: nonemployee in data.nonemployees --><option ng-repeat="nonemployee in data.nonemployees" ng-value="6" ng-selected="false" class="ng-binding ng-scope" value="number:6">
            Division of Program Budget Services
        </option><!-- end ngRepeat: nonemployee in data.nonemployees -->
    </select>

所以在上面,第三个选项有&#34; ng-selected == true&#34;所以我们的ng选择表达式正常工作。

我们有来自nonmployee.id的ng-value给我们ng值(1,2,3,4,5,6)。 我们将ng-model绑定到&#34; data.nonEmployeeCase.nonEmployeeId&#34;在控制器中。 如果我更改选择选项,则更新模型值。大。

问题是ng值应该在选择时工作,以便在加载执行时立即选择正确的表达式并更新模型。但事实并非如此。它取自&#34; data.nonEmployeeCase.nonEmployeeId&#34;的模型值。并使用它来设置选择的正确选项。

这在其他地方对我有用,但在这里不起作用。 有谁看到这个问题? 有什么想法吗?

它似乎正好相反。 而不是ng-value和ng-selected协同工作以选择正确的选项标签然后更新模型,模型正在初始化所选选项。之后,您可以选择其他选项并更新模型。那样太好了。但我们需要以另一种方式进行初始化。

2 个答案:

答案 0 :(得分:0)

我不知道为什么这种方法对我不起作用,就像在其他地方一样。但是我用Option标签上的ng-init修复了它。

<select id="nonemployeeSelect" 
        ng-model="data.nonEmployeeCase.nonEmployeeId">
    <option ng-repeat="nonemployee in data.nonemployees"
            ng-init="nonemployee.id == @ViewBag.nonempId ? data.nonEmployeeCase.nonEmployeeId = nonemployee.id : null"
            ng-value={{nonemployee.id}}>
        {{nonemployee.organization.displayValue}}
    </option>
</select>

答案 1 :(得分:0)

文档警告#include<stdio.h> #include<conio.h> #include<string.h> #include<process.h> #include<stdlib.h> #include<dos.h> struct contact { long ph; char name[20],add[20],email[30]; } list; char query[20],name[20]; FILE *fp, *ft; int i,n,ch,l,found; int main() { main: system("cls"); /* ************Main menu *********************** */ printf("\n\t **** Welcome to ABIGAIL's contact Manager ****"); printf("\n\n\n\t\t\tMAIN MENU\n\t\t=====================\n\t\t[1] Add a new Contact\n\t\t[2] List all Contacts\n\t\t[3] Search for contact\n\t\t[4] Edit a Contact\n\t\t[5] Delete a Contact\n\t\t[0] Exit\n\t\t=================\n\t\t"); printf("Enter the choice:"); scanf("%d",&ch); switch(ch) { case 0: printf("\n\n\t\tAre you sure u want to exit?"); break; /* *********************add new contacts************ */ case 1: system("cls"); fp=fopen("contact.dll","a"); for (;;) { fflush(stdin); printf("To exit enter blank space in the name input\nName (Use identical):"); scanf("%[^\n]",&list.name); if(stricmp(list.name,"")==0 || stricmp(list.name," ")==0) break; fflush(stdin); printf("Phone:"); scanf("%ld",&list.ph); fflush(stdin); printf("address:"); scanf("%[^\n]",&list.add); fflush(stdin); printf("email address:"); gets(list.email); printf("\n"); fwrite(&list,sizeof(list),1,fp); } fclose(fp); break; /* *********************list of contacts************************* */ case 2: system("cls"); printf("\n\t\t================================\n\t\t\tLIST OF CONTACTS\n\t\t================================\n\nName\t\tPhone No\t Address\t\tE-mail ad.\n=================================================================\n\n"); for(i=97;i<=122;i=i+1) { fp=fopen("contact.dll","r"); fflush(stdin); found=0; while(fread(&list,sizeof(list),1,fp)==1) { if(list.name[0]==i || list.name[0]==i-32) { printf("\nName\t: %s\nPhone\t: %ld\nAddress\t: %s\nEmail\t: %s\n",list.name, list.ph,list.add,list.email); found++; } } if(found!=0) {printf("=========================================================== [%c]-(%d)\n\n",i-32,found); getch();} fclose(fp); } break; /* *******************search contacts********************** */ case 3: system("cls"); do { found=0; printf("\n\n\t..::CONTACT SEARCH\n\t===========================\n\t..::Name of contact to search: "); fflush(stdin); scanf("%[^\n]",&query); l=strlen(query); fp=fopen("contact.dll","r"); system("cls"); printf("\n\n..::Search result for '%s' \n===================================================\n",query); while(fread(&list,sizeof(list),1,fp)==1) { for(i=0;i<=l;i++) name[i]=list.name[i]; name[l]='\0'; if(stricmp(name,query)==0) { printf("\n..::Name\t: %s\n..::Phone\t: %ld\n..::Address\t: %s\n..::Email\t:%s\n",list.name,list.ph,list.add,list.email); found++; if (found%4==0) { printf("..::Press any key to continue..."); getch(); } } } if(found==0) printf("\n..::No match found!"); else printf("\n..::%d match(s) found!",found); fclose(fp); printf("\n ..::Try again?\n\n\t[1] Yes\t\t[0] No\n\t"); scanf("%d",&ch); }while(ch==1); break; /* *********************edit contacts************************/ case 4: system("cls"); fp=fopen("contact.dll","r"); ft=fopen("temp.dat","w"); fflush(stdin); printf("..::Edit contact\n===============================\n\n\t..::Enter the name of contact to edit:"); scanf("%[^\n]",name); while(fread(&list,sizeof(list),1,fp)==1) { if(stricmp(name,list.name)!=0) fwrite(&list,sizeof(list),1,ft); } fflush(stdin); printf("\n\n..::Editing '%s'\n\n",name); printf("..::Name(Use identical):"); scanf("%[^\n]",&list.name); fflush(stdin); printf("..::Phone:"); scanf("%ld",&list.ph); fflush(stdin); printf("..::address:"); scanf("%[^\n]",&list.add); fflush(stdin); printf("..::email address:"); gets(list.email); printf("\n"); fwrite(&list,sizeof(list),1,ft); fclose(fp); fclose(ft); remove("contact.dll"); rename("temp.dat","contact.dll"); break; /* ********************delete contacts**********************/ case 5: system("cls"); fflush(stdin); printf("\n\n\t..::DELETE A CONTACT\n\t==========================\n\t..::Enter the name of contact to delete:"); scanf("%[^\n]",&name); fp=fopen("contact.dll","r"); ft=fopen("temp.dat","w"); while(fread(&list,sizeof(list),1,fp)!=0) if (stricmp(name,list.name)!=0) fwrite(&list,sizeof(list),1,ft); fclose(fp); fclose(ft); remove("contact.dll"); rename("temp.dat","contact.dll"); break; default: printf("Invalid choice"); break; } printf("\n\n\n..::Enter the Choice:\n\n\t[1] Main Menu\t\t[0] Exit\n"); scanf("%d",&ch); switch (ch) { case 1: goto main; case 0: break; default: printf("Invalid choice"); break; } return 0; } 不应与ng-selected指令和select指令一起使用。

  
    

注意: ng-model不与ngSelectedselect指令互动,它只在元素上设置ngModel属性。如果您在选择中使用selected,则不应在选项上使用ngModel,因为ngSelected会设置选择值和所选选项。

  
     

— AngularJS ng-selected Directive API Reference

有关详细信息,请参阅