我正在尝试将字符串传递到我的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'));
}
该ng-click
指令需要一个表达式。您传入了您认为是插值的字符串。
您只能{{ the return value of an expression/variable }}
在模板中使用插值(),而不能在表达式中使用。
因此,正确的做法是ng-click="favorites(data.title)"
。
在HTML模板中使用表达式
通常,在AngularJS的HTML模板中执行表达式时,您可以像平常一样访问范围变量,而无需添加$scope
前缀。
因此,如果在您的控制器中您有类似以下内容:
$scope.title = 'News From Today';
您可以像这样绑定到它(表达式,对吗?):
<h1 ng-bind="title"></h1>
或像这样通过插值:
<h1>{{ title }}</h1>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句