Have a small search app using AngularJS and Elasticsearch. Trying to convert my the app from using $scope
to controller As
syntax. I'm using UI Router for my routes/states. Been trying to use
controller: 'HomeCtrl',
controllerAs: 'home'
in my state declarations. And then var vm = this;
in controller to bind to the scope.
I switched ng-model="searchTerms"
on my search input to ng-model="home.searchTerms"
and everywhere else bindings would be needed. None of it works?
Is it better to use ng-controller="HomeCtrl as home"
in a parent div? Is that best practice? Will it work with UI Router?
UPDATE
I now have
var vm = this;
vm.searchTerms = searchTerms;
BUT it still does not work, I keep getting this error in Chrome console
Uncaught ReferenceError: searchTerms is not defined(…)
UPDATED CONTROLLER
'use strict';
angular.module("searchApp.autocomplete", [])
.controller('HomeCtrl', ['$sce', '$state', '$stateParams', 'searchService', function($sce, $state, $stateParams, searchService) {
var vm = this;
var searchTerms = searchTerms;
vm.searchTerms = searchTerms;
vm.noResults = false;
vm.isSearching = false;
vm.results = {
searchTerms: null,
documentCount: null,
documents: [],
queryTime : null,
searchTermGrams: null,
itemsPerPage: 10,
maxResults: 100
};
//autocomplete
vm.autocomplete = {
suggestions: [],
corrections: []
};
vm.showAutocomplete = false;
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…