【发布时间】:2020-03-03 05:54:42
【问题描述】:
下面的简单 HTML 块循环遍历 JSON 格式的人员对象列表 显示他们的名字和姓氏。 ng-repeat 上方的两个输入框相应地链接到基于 firstName 和 lastName 的过滤器。到目前为止一切都很好,工作正常。
我的目标是在用户离开页面时在控制器的过滤器字段中捕获用户输入,然后在他们返回页面时重新填充过滤器。当页面离开时,我可以使用 $scope.$on("$destroy"... 捕获/获取搜索字段值。但是,当我尝试设置这些字段时,$scope.search.firstName = ' John' 我收到一个错误:$scope.search 未定义。我的目标是使用 cookie 来保留然后重置过滤器字段值,但我不确定为什么该设置不起作用以及如何修复它。
<table>
<tr>
<td><input type="text" ng-model="search.firstName" /></td>
<td><input type="text" ng-model="search.lastName" /></td>
</tr>
<tr ng-repeat="person in personList | filter:search:strict">
<td>
<span class="col-xs-offset-2">
{{person.firstName}}
</span>
</td>
<td>
<span class="col-xs-offset-2">
{{person.lastName}}
</span>
</td>
</tr>
</table>
//来自控制器的片段
$scope.search.firstName = 'John'; // this does not work, returns error
$scope.$on("$destroy", function () {
console.log($scope.search.firstName); // this works
console.log($scope.search.lastName)); // this works
});
【问题讨论】:
标签: html angularjs cookies filter angularjs-ng-repeat