下面的简单HTML块循环浏览JSON格式的人员对象列表 显示他们的名字和姓氏。 ng-repeat上方的两个输入框分别链接到基于firstName和lastName的过滤器。到目前为止,一切都很好,正在工作。
我的目标是在用户离开页面时在控制器的过滤器字段中捕获用户输入,然后在返回页面时重新填充过滤器。当页面离开时,我可以使用$ scope。$ on(“ $ destroy” ...来捕获/获取搜索字段值。但是,当我尝试设置这些字段时,$ scope.search.firstName ='约翰,我收到一个错误消息:$ 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
});