AngularJS-单击按钮后专注于文本框

时间:2019-07-15 15:53:26

标签: html angularjs twitter-bootstrap

我一直在网上寻找一些示例,但我在网上找到了不同的版本,但没有一个在起作用。我不会否认自己做错了,但希望有人能帮忙吗?顾名思义,我想在单击按钮后自动关注文本框(学校科目的名称)。

代码如下:

HTML:

<div class="modal-header">
    <h3 class="modal-title" ng-show="isNew">Create New Subject</h3>
    <h3 class="modal-title" ng-hide="isNew">Modify Subject "{{targetEntity.name}}"</h3>
</div>
<div class="modal-body">
    <ng-include src="'views/partials/notification-area.html'"></ng-include>
    <form class="form-horizontal" name="subjectDetailsForm">
        <fieldset>
            <div class="form-group"
                 ng-class="{ 'has-error' : subjectDetailsForm.name.$invalid && !subjectDetailsForm.name.$pristine }">
                <label for="name"
                       class="col-lg-4 col-sm-4 col-xs-4 control-label no-padding-right">Name* </label>
            **<div ng-app="Myapp" ng-controller="txtbox as get" ng-init="get.txtfocus()" class="col-lg-7 col-sm-7 col-xs-7">**
                    <input type="text" class="form-control" id="name" name="name" ng-model="targetEntity.name"
                           required placeholder="Name" ng-maxlength="64">
                    <p ng-show="subjectDetailsForm.name.$invalid && !subjectDetailsForm.name.$pristine"
                       class="help-block">Mandatory field, maximum length 64 characters.</p>
                </div>
            </div> 
        </fieldset>
    </form>
</div>
<div class="modal-footer">
     <button class="btn btn-blue" ng-click="handleCreate()" ng-show="isNew"
             ng-disabled="siteUserDetailsForm.$invalid" auto-focus='name'>
        Create New Subject
    </button>
</div>

AngularJS:

app.controller('SubjectDetailsCtrl', function ($scope, $state, $stateParams, $compile, $uibModal, $log, SpringDataRestService, NgTableParams) {

    $scope.renderCrudEntityState = renderCrudEntityState;
    $scope.renderEntityState = renderEntityState;

    **angular.module('Myapp',[]).controller('txtbox', function() {
    var get = this;
       get.txtfocus = function () {
          document.getElementById('name').focus();
          document.getElementById('name').style.color='Red'
       };
    });**

    // Populate data table with subject array
    $scope.refreshTable = function () {
        SpringDataRestService.get(
            {
                resource: "subjects"
            },
            function (response) {
                $scope.subjectTableOptions = new NgTableParams({}, {
                    dataset: response.subjects,
                    counts: [], // hide page counts control
                });
            },
            function (response) {
                clearDentAlerts($scope.alerts);
                reportDentAlert($scope.alerts, new DentAlert(AlertType.ERROR, generateAlertMessage(response)));
            }
        );
    };

    $scope.refreshTable();

    $scope.openModal = function (row) {
        var uibModalInstance = $uibModal.open({
            windowClass: "",
            templateUrl: "views/modals/subject-details-edit.html",
            controller: 'ModalSubjectDetailsEditCtrl',
            backdrop  : 'static',
            size: null,
            resolve: {
                row: function () {
                    return row;
                },
                onComplete: function () {
                    return $scope.refreshTable;
                }
            }
        });
    };
});

视频参考:https://www.youtube.com/watch?v=r79qiKjonEA

0 个答案:

没有答案
相关问题