【问题标题】:disable typeahead angular controller dependent on user setting根据用户设置禁用预先输入的角度控制器
【发布时间】:2015-02-18 12:19:26
【问题描述】:

当用户在设置菜单中选中复选框时,我想禁用预先输入功能 (id = searchSuggestions)。下面的代码仅在页面重新加载时有效,但在会话期间无效。是否可以在会话期间禁用 Angular 的预输入实现?如果有,怎么做?

// Controller for Angular's Search implementation
app.controller('TypeaheadCtrl', function($scope, $http) {
    $scope.selected = undefined;
    // prefetch the results
    var value = $('#searchSuggestions').is(':checked');
    if (value == true) {
      $.getJSON('/typeahead', function(response){
        //parse your response and assign it
        $scope.recentPopularQueries = response;
       });

      // Fire off the transcribe(words) function as soon as a choice is selected
      $scope.onSelect = function ($item, $model, $label) {
        $scope.$label = $label;
        transcribe($scope.$label);
      };
    }
  });

【问题讨论】:

    标签: angularjs angular-ui-typeahead


    【解决方案1】:

    使用范围模型创建一个复选框:

    <input type="checkbox" ng-model="typeaheadActive">
    

    在执行 typeahead 请求之前将模型值添加到您的条件:

    // Controller for Angular's Search implementation
    app.controller('TypeaheadCtrl', function($scope, $http) {
        $scope.selected = undefined;
        // prefetch the results
        var value = $('#searchSuggestions').is(':checked');
        if (value == true && typeaheadActive) {
            $.getJSON('/typeahead', function(response) {
                //parse your response and assign it
                $scope.recentPopularQueries = response;
            });
    
            // Fire off the transcribe(words) function as soon as a choice is selected
            $scope.onSelect = function ($item, $model, $label) {
                $scope.$label = $label;
                transcribe($scope.$label);
            };
        }
    });
    

    【讨论】:

    • 谢谢 - 这看起来很简单!但是,现在它抱怨它无法识别 typeaheadActive: ReferenceError: typeaheadActive is not defined
    • 尝试在控制器的开头定义typeahead = true;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-04
    • 1970-01-01
    • 2011-04-25
    • 2014-06-29
    • 2020-09-09
    • 2021-01-17
    • 2016-01-14
    相关资源
    最近更新 更多