我一生都无法弄清楚如何让角带弹出框在返回的 JSON 内容中工作(数据属性在 JSON 文件中)。
Plunkr:http ://plnkr.co/edit/jVmHwIwJ0KOKCnX6QjVa?p=preview
任何见解将不胜感激。非常感谢!
HTML
<!-- Search -->
<div class="well">
<p>Search the term "content"</p>
<form role="form">
<div my-search ng-model="selectedContent" class="form-group clearfix search">
<input type="text" ng-model="selectedContent" ng-options="query as query.searchQuery for query in searchData" bs-typeahead="bs-typeahead" class="form-control search-field"/>
<button type="button" class="btn btn-primary search-btn" ng-click="updateModel()"><span class="glyphicon glyphicon-search"></span></button>
</div>
</form>
</div>
<!-- Dynamic Content -->
<div class="well">
<h4>{{clickedContent.contentTitle}}</h4>
<ul>
<li ng-repeat="item in clickedContent.headlines" ng-bind-html="item.headline"></li>
</ul>
</div>
JSON
[
{
"contentId": 1,
"searchQuery": "Content set 1 dummy query vestibulum abcdefghijklmnop",
"contentTitle": "Pretaining to content set 1",
"popoverTitle": "Query info",
"popoverContent": "Interesting info about query",
"headlines": [
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>1st headline in content set 1</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>2nd headline in content set 1</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>3rd headline in content set 1</a>"
}
]
},
{
"contentId": 2,
"searchQuery": "Content set 2 dummy query vestibulum abcdefghijklmnop",
"contentTitle": "Pretaining to content set 2",
"popoverTitle": "Query info",
"popoverContent": "Interesting info about query",
"headlines": [
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>1st headline in content set 2</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>2nd headline in content set 2<a/>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>3rd headline in content set 2</a>"
}
]
},
{
"contentId": 3,
"searchQuery": "Content set 3 dummy query vestibulum abcdefghijklmnop",
"contentTitle": "Pretaining to content set 3",
"popoverTitle": "Query info",
"popoverContent": "Interesting info about query",
"headlines": [
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>1st headline in content set 3</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>2nd headline in content set 3</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>3rd headline in content set 3</a>"
}
]
},
{
"contentId": 4,
"searchQuery": "Content set 4 dummy query vestibulum abcdefghijklmnop",
"contentTitle": "Pretaining to content set 4",
"popoverTitle": "Query info",
"popoverContent": "Interesting info about query",
"headlines": [
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>1st headline in content set 4</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>2nd headline in content set 4</a>"
},
{
"headline": "<a href='#' data-placement='bottom', data-trigger='hover' data-title='Headline details' data-content='details about headline' bs-popover>3rd headline in content set 4</a>"
}
]
}
]
JS
var app = angular.module('demoApp', ['ngAnimate', 'ngSanitize', 'mgcrea.ngStrap'])
.config(function ($typeaheadProvider) {
angular.extend($typeaheadProvider.defaults, {
template: 'ngstrapTypeahead.html',
container: 'body'
});
});
app.directive('mySearch', function(){
return {
restrict: 'A',
require: 'ngModel',
link: function($scope, $element, $attrs, ngModel){
ngModel.$render = function(){
if (angular.isObject($scope.selectedContent)) {
$scope.clickedContent = $scope.selectedContent;
}
}
$scope.updateModel = function() {
$scope.clickedContent = $scope.selectedContent;
}
}
}
})
function MainController($scope, $templateCache, $http) {
$scope.selectedContent = '';
$http.get('searchData.json').then(function(response){
$scope.searchData = response.data;
return $scope.searchData;
});
};