首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将注意力集中在角度模板/路径/形式的文本输入上

将注意力集中在角度模板/路径/形式的文本输入上
EN

Stack Overflow用户
提问于 2015-02-27 04:21:01
回答 1查看 1.6K关注 0票数 1

我有我的应用程序的根页面,这是一个搜索页面。我也有一个div,当一个按钮被点击时,它会在页面上滑动,在这个区域中,我有一个收藏夹列表。这个面板里的一切都是棱角分明的。

当url为"#/“时,面板的第一个屏幕是文章,而包含Add收藏夹形式的第二个屏幕可以通过角路由"#/newsearch”访问。所有这些都很好。密码在下面。

如何将焦点放在添加收藏夹表单中的特定输入上?换句话说,当导航到角路由"#/newsearch“时,我希望输入名为" name”的字段成为焦点字段,这样他们就可以开始键入最喜欢的字段的名称了。

我读过其他一些堆栈溢出问题,但是没有什么能作为我的场景的简单解决方案。

角模块:

/ng-modules/render-index.js

代码语言:javascript
复制
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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-27 04:57:21

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

代码语言:javascript
复制
<input type="text" ... autofocus/>

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

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28757476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档