将焦点设置为角度模板/路线/表单中的文本输入

Set focus on text input in angular template/route/form

本文关键字:表单 路线 输入 文本 设置 焦点      更新时间:2024-06-22

我有我的应用程序的根页面,它是一个搜索页面。我还有一个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>&nbsp;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属性。