簡體   English   中英

如何在Angularjs的contextscope中傳遞從下拉列表中選擇的值?

[英]How to pass the value selected from dropdown in contextscope in Angularjs?

我有一個下拉列表

$scope.animals = [
      {value:'lion', name:'lion', description: 'lion'}, 
      {value:'cat', name:'cat', description: 'cat'}, 
      {value:'dog', name:'dog', description: 'dog'}, 
    ];

我想將從dropdown中選擇的值作為contextscope傳遞給另一個指令。 例如

<md-select ng-model="context">
    <md-option ng-repeat="animal in animals" value="{{animal.value}}"  aria-label="{{animal.name}}">{{animal.name}}</md-option>
</md-select>
<div described-type flex="70" contextscope="context"></div>

在describe-type指令中,我正在添加描述

<input ng-model="context.description" name="context.description">

我收到以下錯誤:“無法在字符串'lion'上創建屬性'描述'”

我想要的JSON格式是

"lion" : {
           "description": "wild animal"
         }

如何解決錯誤並創建JSON?

你設置的值不正確。 ngModel設置為value,您設置為animal.value ,在這種情況下是字符串lion。 相反,它應該是:

<md-select ng-model="context">
    <md-option ng-repeat="animal in animals" value="{{animal}}"  aria-label="{{animal.name}}">{{animal.name}}</md-option>
</md-select>
<div described-type flex="70" contextscope="context"></div>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM