angularjs - 通过 Controller 显示输入元素后选择文本框中的文本

标签 angularjs

我正在寻找一种 AngularJS 友好的方式来显示输入框并选择输入中的文本。我尝试使用 Controller 添加逻辑,这是我到目前为止所拥有的:

HTML ComparisonCenterProfiles.html

<div id="saved-profiles" class="select-list" ng-cloak>
    <ul>
        <li ng-repeat="profile in profiles" ng-class="{'ellipsis': true, 'active': isActive(profile.CompSetId)}">
            <span class="save-disk" ng-click="saveActiveProfile()" ng-show="isActive(profile.CompSetId) && profile.IsDirty" style="cursor: pointer;"></span>

            <a href="javascript:;" ng-show="!forceRenameProfile || !isActive(profile.CompSetId)" ng-click="setActiveProfile(profile)">{{profile.CompSetName}}</a>

            <input type="text" ng-model="profile.CompSetName" ng-show="forceRenameProfile && isActive(profile.CompSetId)" ng-blur="saveActiveProfile()" />

        </li>
    </ul>
</div>

Javascript

angular
.module('app')
.directive('compProfiles', ['pageMethod', function (pageMethod) {
    return {
        restrict: 'E',
        require: '^compEditor',
        controller: ['$scope', function ($scope) {
            $scope.saveActiveProfile = function () {
                if ($scope.activeProfile.CompSetName.length < 3) {
                    showAlert('Error',
                                '<strong>' + $scope.activeProfile.CompSetName + '</strong> is not a valid profile name. Your profile name must have at least 3 characters.',
                                [
                                    {
                                        text: 'OK',
                                        click: function () {
                                            $scope.forceRenameProfile = true;
                                            hideAlert($(this));
                                        }
                                    }
                                ]);
                }
                else {
                    continueSavingProfile();
                    $scope.forceRenameProfile = false;
                }
            }
        }],
        templateUrl: 'Templates/ComparisonCenterProfiles.html'
    };
}]);

所以此时,我可以显示输入框,但无法实际选择输入框中的文本,以强调用户需要更改框中的内容。

旁注:我是 AngularJS 的新手,并且来自 jQuery 背景,我当然尝试在元素上使用 focus() 和 select() 但无济于事。

任何想法将不胜感激!

最佳答案

您可以从链接指令中获取输入元素。将此函数添加到指令中:

link: function(scope, element) {
   var inputElement = element.find('input');
   scope.selectText = function() {
       inputElement.setSelectionRange(0, inputElement.value.length)
   }
}

在你的 Controller 中你可以调用 selectText 函数:

scope.selectText();

关于angularjs - 通过 Controller 显示输入元素后选择文本框中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37600354/

相关文章:

jasmine - 如何在 jasmine 测试中为 Angular 项目注入(inject)依赖项

javascript - 如何在多个 Controller 中使用相同的数据?

javascript - 将对象绑定(bind)到单选按钮组时缺少功能和错误

angularjs - Angular NgUpgrade 将 AngularJS 服务注入(inject) Angular 服务;获取 : Unhandled Promise rejection: Cannot read property 'get' of undefined ; Zone:

angularjs - Angular HTTP : Status -1 and CORS

javascript - 在 Angular 中通过 $scope 安全地使用 eval()

javascript - 将 Jquery 转换为 Angular 指令

javascript - 将 Angular.js 绑定(bind)传递给 javascript 函数

javascript - 应用 Bootstrap 时,选择下拉菜单在移动设备和 iPad View 中不起作用

javascript - 在单击按钮时将选定的行数据作为参数传递