2014-10-08 23 views
1
error : Error: [$parse:syntax] Syntax danger: Token '}' is unexpected, expecting [:] at column 35 of the expression [yourSelectRadio={item.Polarisation}] starting at [}]. 
http://errors.angularjs.org/1.2.21/$parse/syntax?p0=%7D&p1=is%20unexpected%2C%20expecting%20%5B%3A%5D&p2=35&p3=yourSelectRadio%3D%7Bitem.Polarisation%7D&p4=%7D 

我的代碼的35列:語法危險:令牌 '}' 是出乎意料的,希望[:]在表達式[yourSelectRadio = {item.Polarisation}]

<div class="btn-group" ng-init="yourSelectRadio={item.Polarisation}"> 
<label class="btn btn-success ng-pristine ng-valid" ng-model="yourSelectRadio" btn-radio="'Vertical'">Vertical</label> 
<label class="btn btn-success ng-pristine ng-valid" ng-model="yourSelectRadio" btn-radio="'Horizontal'">Horizontal</label> 
</div> 

爲什麼發生這種錯誤?

+0

'yourSelectRadio = {伊特m.Polarisation}'是無效的javascript。 – 2014-10-08 14:29:10

+0

我該怎麼辦? – 2014-10-08 14:32:07

+0

...使其有效。鑑於無效的JavaScript,我不知道你的意圖是什麼,因此我不能建議如何「修復它」 – 2014-10-08 14:32:34

回答

2

{}是你需要一個關鍵的項目

{ 'key' : item.Polarisation } ? 

yourSelectRadio=item.Polarisation // if it is an object 

編輯對象:

ng-init功能需要的對象。 JSON被定義爲 { "key" : "value" }{ [OBJECT] }

假設item.Polarisation ~= { "thing" : "one", "otherthing" : 2 }

有兩種方法來修復錯誤。

直接分配

<div class="btn-group" ng-init="yourSelectRadio=item.Polarisation"> 

- 或 -

封裝

<div class="btn-group" ng-init="yourSelectRadio={'radioItem': item.Polarisation}"> 
+0

謝謝@KevinB,報價是這樣的,他不會混淆鍵與項目對象他參考。 – corn3lius 2014-10-08 15:22:28

+0

這個答案沒有意義。你能寫出一個更完整的例子嗎? – Aerovistae 2015-04-20 20:11:06

+1

@Aerovistae做的編輯爲你清除它? – corn3lius 2015-04-21 15:08:08

0

使用angular.toJson(對象):

<div class="btn-group" ng-init="yourSelectRadio=angular.toJson(item.Polarisation)"> 
     <label class="btn btn-success ng-pristine ng-valid" ng-model="yourSelectRadio" btn-radio="'Vertical'">Vertical</label> 
     <label class="btn btn-success ng-pristine ng-valid" ng-model="yourSelectRadio" btn-radio="'Horizontal'">Horizontal</label> 
</div>