簡體   English   中英

即使可以看到對象的內容,也無法訪問其屬性

[英]I can't access the properties of an object even though I can see it's contents

當我使用引導程序的下拉菜單方式時,此代碼過去工作得很好。.既然我切換到常規模式,我不知道為什么會遇到這個問題。 為什么我不能訪問屬性? 瘋了我

HTML

    <select class="form-control" ng-model="client" ng-change="clientSelect(client)">
        <option value="">--- Please Select ---</option>
        <option ng-repeat="client in clientList" value="{{client}}">{{client.clientName}}</option>
    </select>

角/ JavaScript的

$scope.clientSelect = function (client) {
  console.log(client);
  console.log(client.clientName);  //is undefined
  console.log(client.clientId);  //is undefined
}

輸出繼電器:

{"clientId":102,"clientName":"Testing"}  //i clearly see the properties here...
UsersController.js:130 undefined
UsersController.js:131 undefined

編輯:當我console.log($ scope.clientList)很好..數組中第一項的對象看起來像這樣:

0:對象$$ hashKey:“對象:254” clientId:102 clientName:“ Testing” _proto :對象

您無法訪問屬性,因為從ng-repeat獲得的clientstring

檢查我用您的代碼創建的這個Plunker的控制台日志。 您將看到第一個consol.log(client)實際上是注銷了以下字符串: {"clientId":102,"clientName":"Testing 1"} 因此,它沒有任何屬性。

選擇的文檔

請注意,不帶ngOptions使用的select指令的值始終是字符串。 當模型需要綁定到非字符串值時,您必須使用指令(參見下面的示例)將其明確轉換,或使用ngOptions指定選項集。 這是因為選項元素目前只能綁定到字符串值。

考慮將ngOptions指令而不是<select>與ng-repeat一起使用。

暫無
暫無

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

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