【发布时间】:2016-05-18 00:38:23
【问题描述】:
我正在尝试将字符串传递给我的 javascript 文件中的函数
变量 {{data.title}} 可以在我的 html 文件的其他地方访问,但我似乎遗漏了一些东西,因为函数没有将变量接收为 ''
<a class="item item-icon-left" ng-click="favorites('{{data.title}}')">
<i class="icon ion-ios-star-outline"></i>
<span>Add to favorites</span>
</a>
JS 文件:
$scope.favorites = function (x) {
console.log(x);
// retrieve it (Or create a blank array if there isn't any info saved yet),
var favorites = JSON.parse(localStorage.getItem('favoritesInfo')) || [];;
// add to it,
favorites.push({ name: x});
// then put it back.
localStorage.setItem('favoritesInfo', JSON.stringify(favorites));
console.log(localStorage.getItem('favoritesInfo'));
}
【问题讨论】:
-
为什么要将参数“data.title”(已经是字符串)用单引号括起来?您不能在表达式中使用插值,只能在 HTML 模板中使用。试试
favorites(data.title)。不确定这是否能解决您的问题,但这是一个良好实践的说明。如果有帮助,请告诉我,我会将其设置为答案。
标签: javascript html angularjs firebase