ReferenceError:控制台中的左侧无效分配

时间:2016-09-01 07:18:15

标签: javascript jquery css angularjs

Js代码

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);

我在最后一行动态地将subMDesign附加到div。在控制台中,subMDesign显示如下:

enter image description here

在拖动开始时,控制台中显示的错误如下:

  

ReferenceError:左侧无效分配   event.dataTransfer.setData(text,drag.action?reportId = 93&amp; reportType = 6_93_6_22)

和箭头标记指向reportId = 93。请帮我解决这个问题。

1 个答案:

答案 0 :(得分: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);

您会看到我添加转义引号(例如:\')以阻止它们终止真实字符串,但会导致引用在页面上呈现。< / p>

例如:

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'