我被这个问题困住了。
我有一个使用 html5 历史 API 的搜索页面。
因此,如果我搜索“html5”,我的网址就是“/search/html5”。
现在我希望我的应用程序能够处理页面刷新。当我按“F5”时,我希望看到我的应用程序用“html5”填充文本输入并触发单击按钮以启动搜索方法。
<form>
<input type="text" ng-model="searchText" />
<button class="btn btn-primary" ng-disabled="!searchText" ng-click="search()" >search</button>
</form>
我真的很感激一些帮助。
UPD:我的意思是我需要手动执行(在 Angular Controller 之外)类似
$scope.searchText = 'html5';
$scope.search();
但我不知道如何访问 $scope。
最佳答案
还有另一个很好的解决方案。
我喜欢它,因为它是原生的,不需要任何第三方库。
只需将其添加到 html 中的某个位置(同样,我使用 ejs 进行服务器模板):
<p ng-init="searchText = '<%= searchText %>'; search();"></p>
这种方式比那种方式更好https://stackoverflow.com/a/13232922/801426因为使用后者时,输入的值在搜索完成之前不会更新,而且它是一个 ajax 调用,因此需要一些时间。
关于angularjs - 设置初始值并触发点击 AngularJS 中的应用程序初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13227391/