将焦点设置为角度模板/路线/表单中的文本输入
Set focus on text input in angular template/route/form
我有我的应用程序的根页面,它是一个搜索页面。我还有一个div,当点击按钮时,它会在页面上滑动,在这个区域中我有一个收藏夹列表。这个面板里面的一切都是棱角分明的。
当url为"#/"时,面板的第一个屏幕是文章,而包含"添加收藏夹"表单的第二个屏幕可以通过角度路径"#/newsearch"访问。所有这些都很好。代码如下。
如何将焦点放在"添加收藏夹"表单中的特定输入上?换句话说,当导航到角度路径"#/newsearch"时,我希望名称为"name"的输入字段成为焦点字段,这样他们就可以开始键入收藏夹的名称。
我读过其他一些堆栈溢出问题,但对于我的场景来说,没有什么是简单的解决方案。
角度模块:
/ng模块/渲染索引.js
angular
.module("renderIndex", ["ngRoute","ngCookies"])
.config(config)
.controller("favoritesController", favoritesController)
.controller("newFavoriteController", newFavoriteController);
function config($routeProvider) {
$routeProvider
.when("/", {
templateUrl: "/ng-templates/favoritesView.html",
controller: "favoritesController",
controllerAs: "vm"
})
.when("/newsearch", {
templateUrl: "/ng-templates/newFavoriteView.html",
controller: "newFavoriteController",
controllerAs: "vm"
})
.otherwise({ redirectTo: "/" });
};
function favoritesController($http) {
var vm = this;
vm.searches = [];
vm.isBusy = true;
$http.get("/api/favorites")
.success(function (result) {
vm.searches = result;
})
.error(function () {
alert('error/failed');
})
.then(function () {
vm.isBusy = false;
});
};
function newFavoriteController($http, $window, $cookies) {
var vm = this;
vm.newFavorite = {};
vm.newFavorite.searchString = $cookies.currentSearch;
vm.newFavorite.userId = $cookies.uId;
vm.save = function () {
$http.post("/api/favorites", vm.newFavorite)
.success(function (result) {
var newFavorite = result.data;
//TODO: merge with existing topics
alert("Thanks for your post");
})
.error(function () {
alert("Your broken, go fix yourself!");
})
.then(function () {
$window.location = "#/";
});
};
};
角度视图:
/ng模板/收藏夹View.js
<div class="small-8 column"><h3>Favorites {{vm.favorites.length}}</h3></div>
<div class="small-4 column"><a class="tiny button radius" href="#/newsearch"><i class="fi-plus"></i> Add</a></div>
<div class="small-12 column">
<div class="favContent">
<div class="search row" data-ng-repeat="s in vm.searches">
<div class="favName small-10 column"><span data-tooltip aria-haspopup="true" class="has-tip" title="{{s.description}}"><a href="{{s.searchString}}">{{s.name}}</a></span></div>
<div class="small-2 column"><i class="fi-trash"></i></div>
</div>
</div>
</div>
角度视图:
/ng模板/newFavoriteView.js
<div class="small-8 column"><h3>Saving Search</h3></div>
<div class="small-12 column">
<form name="newFavoriteForm" novalidate ng-submit="vm.save()">
<input name="userId" type="hidden" ng-model="vm.newFavorite.userId" />
<input name="searchString" type="hidden" ng-model="vm.newFavorite.searchString" />
<label for="name">Name</label>
<input name="name" type="text" ng-model="vm.newFavorite.name"/>
<label for="description">Description</label>
<textarea name="description" rows="5" cols="30" ng-model="vm.newFavorite.description"></textarea>
<input type="submit" class="tiny button radius" value="Save" /> | <a href="#/" class="tiny button radius">Cancel</a>
</form>
</div>
如果您知道需要关注哪个输入,您只需将其添加到其中一个即可。
<input type="text" ... autofocus/>
如果您需要根据其他变量更改哪一个获得焦点,那么您可能需要向表单添加一个自定义指令,该指令可以查看表单上的输入,然后添加autofocus
属性。
相关文章:
- 可以't让我的if语句处理js中的html表单输入
- 如何使用WCF服务和javascript表单post上传.doc文件
- Javascript生成的表单未提交
- 如何使用javascript或html下载PDF格式的填写表单
- HTML表单提交时未执行外部函数
- 如何将输入(type=text)从html表单传递到javascript函数
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- Ajax发布表单序列化,发布引号'
- 多级HTML表单
- 后焦点更改为IE 11中的地址栏,而不是转移到表单中的下一个控件
- 在验证和发送邮件后更改联系人表单的 html
- jQuery表单添加不适用于下拉列表
- Rails/JSON:如何将JSON用于jquery UI自动完成表单
- 如何从pdftron webviewer获取表单数据
- JS验证ajax返回的html中的表单数据
- Javascript更新孙窗口中的表单元素
- 将焦点设置为角度模板/路线/表单中的文本输入
- 如何从Ember中的表单提交转换路线
- 保持路线变更AngularJS的表单输入
- $location.path不会更改表单提交后的路线