2016-09-01 62 views
4

JS代碼的ReferenceError:無效轉讓左側控制檯

if ($language == 'english') { 
    var displayvalue = ""; 
    if (data[i].report_name.length >= 20) { 
    displayvalue = data[i].report_name.substr(0, 20) + "..."; 
    } else { 
    displayvalue = data[i].report_name 
    } 
    subMDesign += '<a href="" ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22)"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>'; 

} else { 
    var displayvalue = ""; 
    if (data[i].report_name.length >= 20) { 
    displayvalue = data[i].report_name_ar.substr(0, 20) + "..."; 
    } else { 
    displayvalue = data[i].report_name_ar; 
    } 
    subMDesign += '<a href="" ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22)"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>'; 
} 
} 
var temp = $compile(subMDesign)($scope); 
$("#reportUl" + $catid).html(temp); 

我在動態最後一行追加subMDesigndiv

enter image description here

在拖曳開始,在控制檯中顯示錯誤如下::

ReferenceError: invalid assignment left-hand side event.dataTransfer.setData(text, drag.action?reportId=93&reportType=6_93_6_22)

和箭頭標記指出reportId = 93在控制檯中,如下subMDesign顯示。請幫我解決這個問題。

+0

什麼?你知道確切的位置,並且錯誤信息是清楚的。你需要什麼樣的幫助? – Teemu

+0

但我不知道那個位置的問題 – Asha

+0

'drag.action?reportId = 93&reportType = 6_93_6_22'在純JS中做什麼?它看起來像一個URL的一部分,但在你的代碼中它實際上是一個任務。 – Teemu

回答

0

這是因爲你正在分配一個字符串而沒有指出它是這樣的;例如,要渲染這個頁面上:

// Note the quotes, to escape the string: 
event.dataTransfer.setData(text, 'drag.action?reportId=93&reportType=6_93_6_22') 

爲了實現這一目標,這裏有一個簡單的重構也使代碼更容易通過消除重複閱讀。我注意到,英語/非英語之間唯一不同的是,報告名稱字段:

var displayvalue = ""; 
var field = ($language == 'english') ? 'report_name' : 'report_name_ar'; 

if (data[i].report_name.length >= 20) { 
    displayvalue = data[i][field].substr(0, 20) + "..."; 
} else { 
    displayvalue = data[i][field]; 
} 

subMDesign += '<a href="" ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, \'drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22\')"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>'; 

var temp = $compile(subMDesign)($scope); 
$("#reportUl" + $catid).html(temp); 

你會看到,我添加逃脫引號(是這樣的:\'),以防止他們終止實字符串,但會導致報價在頁面上呈現。

例如:

var foo = "foo"; 
console.log(foo); // prints: foo 
var bar = "\"bar\""; // we have to escape double quotes inside double quotes 
console.log(bar); // prints: "bar" 
var blee = "'blee'"; // no need to escape single quotes inside double quotes 
console.log(blee); // prints: 'blee' 
var blar = '\'blar\''; // but we have to escape single quotes inside single quotes 
console.log(blee); // prints: 'blar' 
相關問題