如何将 localStorage 变量插入 ng-repeat 以便用户可以更改过滤器
How do I insert a localStorage variable into ng-repeat so the user can change the filter
现在我有几个按钮可以将值插入 localStorage。我以为我可以获取值并将其插入 ng-repeat 行以更改过滤。
如果我使用普通过滤,这有效:
<div class='segment' ng-repeat="x in names | filter:x.bodyType='Buggy'">
但这不起作用:
<div class='segment' ng-repeat="x in names | filter:x.bodyType='<script>var show = localStorage.getItem('show'); show</script>'">
我没有使用 Jquery 或除 AngularJS 之外的任何其他 JS 或 CSS 框架。不使用 PHP。这对于 Web 应用程序来说很简单 HTML5。
我认为 Angular 无法插入表达式 <script>...</script>
。我建议你在你的控制器中有一个函数,它使用 localStorage 服务获取值,比如:
$scope.bodyType = function() {
localStorage.getItem('show');
};
<div class='segment' ng-repeat="x in names | filter:x.bodyType='{{bodyType()}}'">
动态构建对象并将其传递给过滤器。
$scope.show = 'A';
$scope.base = function() {
return $scope.show;
};
$scope.bodyType = function() {
return {bodyType: $scope.base()};
};
$scope.objs = [{bodyType: 'A'}, {bodyType: 'B'}];
<button ng-click="show='B'">Button</button>
<div ng-repeat="x in objs | filter: bodyType()">{{x}}</div>
现在我有几个按钮可以将值插入 localStorage。我以为我可以获取值并将其插入 ng-repeat 行以更改过滤。
如果我使用普通过滤,这有效:
<div class='segment' ng-repeat="x in names | filter:x.bodyType='Buggy'">
但这不起作用:
<div class='segment' ng-repeat="x in names | filter:x.bodyType='<script>var show = localStorage.getItem('show'); show</script>'">
我没有使用 Jquery 或除 AngularJS 之外的任何其他 JS 或 CSS 框架。不使用 PHP。这对于 Web 应用程序来说很简单 HTML5。
我认为 Angular 无法插入表达式 <script>...</script>
。我建议你在你的控制器中有一个函数,它使用 localStorage 服务获取值,比如:
$scope.bodyType = function() {
localStorage.getItem('show');
};
<div class='segment' ng-repeat="x in names | filter:x.bodyType='{{bodyType()}}'">
动态构建对象并将其传递给过滤器。
$scope.show = 'A';
$scope.base = function() {
return $scope.show;
};
$scope.bodyType = function() {
return {bodyType: $scope.base()};
};
$scope.objs = [{bodyType: 'A'}, {bodyType: 'B'}];
<button ng-click="show='B'">Button</button>
<div ng-repeat="x in objs | filter: bodyType()">{{x}}</div>