отключить угловой контроллер typeahead в зависимости от настройки пользователя

Я хотел бы отключить функцию ввода текста, когда у пользователя установлен флажок в меню настроек (id = searchSuggestions). Приведенный ниже код работает только при новой перезагрузке страницы, но не во время сеанса. Можно ли отключить реализацию углового ввода во время сеанса? Если да, то как?

// 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);
      };
    }
  });

person kd1978    schedule 18.02.2015    source источник


Ответы (1)


Создайте флажок с моделью области действия:

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

Добавьте значение модели к вашему условию перед выполнением запроса на ввод:

// 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);
        };
    }
});
person DonJuwe    schedule 18.02.2015
comment
Спасибо - это выглядит просто! Однако теперь он жалуется, что не распознает typeaheadActive: ReferenceError: typeaheadActive не определен - person kd1978; 18.02.2015
comment
попробуйте определить typeahead = true; в начале вашего контроллера. - person DonJuwe; 18.02.2015