【问题标题】:Set focus on text input in angular template/route/form将焦点放在角度模板/路线/表单中的文本输入上
【发布时间】:2015-02-27 04:21:01
【问题描述】:

我的应用程序的根页面是一个搜索页面。当单击按钮时,我还有一个 div 会在页面上滑入,并且在此区域内我有一个收藏夹列表。此面板中的所有内容都是有角度的。

当 url 为“#/”时,面板的第一个屏幕是文章,而包含添加收藏夹表单的第二个屏幕可通过角度路径“#/newsearch”访问。所有这一切都很好。代码如下。

如何将焦点放在“添加收藏夹”表单中的特定输入上?因此,换句话说,当导航到角度路径“#/newsearch”时,我希望名称为“name”的输入字段成为焦点字段,以便他们可以开始输入收藏夹的名称。

我已经阅读了其他一些堆栈溢出问题,但对于我的场景来说,没有什么是一个简单的解决方案。

角度模块:
/ng-modules/render-index.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-templates/favoritesView.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-templates/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>

【问题讨论】:

    标签: javascript html angularjs focus angular-routing


    【解决方案1】:

    如果您知道需要关注哪个输入,则只需将其添加到其中之一即可。

    <input type="text" ... autofocus/>
    

    如果您需要根据其他变量更改哪个获得焦点,那么您可能需要向您的表单添加一个自定义指令,该指令可以查看表单上的输入,然后添加autofocus 属性。

    【讨论】:

    • 好的,我想我需要编写一个指令并将其与我的模块一起使用。我想随着我得到更多可能需要研究的要求。
    猜你喜欢
    • 2015-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-15
    • 2018-03-25
    • 2019-07-02
    • 2011-12-07
    • 2013-06-09
    相关资源
    最近更新 更多