我对 Angular 比较陌生,我正在尝试将 np-autocomplete 集成到我的应用程序中(https://github.com/ng-pros/np-autocomplete)。但是,当我在 $scope.options 中将 html 字符串作为模板传递时,我只能让它工作,而当我想从单独的 html 加载它时它不起作用。
我的应用程序的代码如下所示:
var eventsApp = angular.module('eventsApp',['ng-pros.directive.autocomplete'])
eventsApp.run(function($templateCache, $http) {
$http.get('test.html', {
cache: $templateCache
});
console.log($templateCache.get('test.html')) // --> returns undefined
setTimeout(function() {
console.log($templateCache.get('test.html')) // --> works fine
}, 1000);
//$templateCache.put('test.html', 'html string') //Would solve my issue in the controller,
//but I would rather prefer to load it from a separate html as I'm trying above
在我的控制器中,我正在设置自动完成的选项,如下所示:
controllers.createNewEventController = function ($scope) {
$scope.options = {
url: 'https://api.github.com/search/repositories',
delay: 300,
dataHolder: 'items',
searchParam: 'q',
itemTemplateUrl: 'test.html', // <-- Does not work
};
//other stuff...
}
但是,似乎 test.html 在 np-autocomplete 想要使用它时是未定义的(因为它也在上面的第一个 console.log 中)。
所以我的直觉告诉我,test.html 可能在加载到 eventsApp.run(...) 之前在控制器中被访问。但是我不确定如何解决这个问题?
任何帮助将不胜感激。