【发布时间】:2014-06-22 06:34:53
【问题描述】:
我可以从应用内的任意链接查看特定 json 数组数据的内容吗?
正如我的演示 plunker 所示,我的搜索工作正常(搜索术语“内容”)并返回我想要的内容;但我也希望能够从应用程序其他地方的某些链接中调用一些返回的数据。
Plunker 场景: 在我的 plunker 中单击“查看内容集 3 的信息”链接应该会在其下方显示内容集 3 中的信息,但我不知道如何让它工作。
Plunker 演示:http://plnkr.co/edit/Z8A8nJ6uQfdTwR2TGRtY?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>
<!-- this link should also return the specified data -->
<a href="">View info for content set 3:</a>
<!-- Dynamic Content -->
<div class="well">
<h4>{{clickedContent.contentTitle}}</h4>
<ul>
<li ng-repeat="item in clickedContent.headlines" ng-bind-html="toTrusted(item.headline)"></li>
</ul>
</div>
AngularStrap Typeahead 部分模板
<ul class="typeahead dropdown-menu" tabindex="-1" ng-show="$isVisible()" role="select">
<li role="presentation" ng-repeat="match in $matches" ng-class="{active: $index == $activeIndex}">
<a href="" role="menuitem" tabindex="-1" ng-click="updateModel(); $select($index, $event)">
<div class="query" ng-bind="match.label" data-title="{{match.value.popoverTitle}}", data-content="{{match.value.popoverContent}}", data-placement="right", data-trigger="hover", bs-popover></div>
</a>
</li>
</ul>
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, $sce, $templateCache, $http) {
$scope.selectedContent = '';
$http.get('searchData.json').then(function(response){
$scope.searchData = response.data;
return $scope.searchData;
});
$scope.toTrusted = function(headlineHtml) {
return $sce.trustAsHtml(headlineHtml)
};
};
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='#'>1st headline in content set 1</a>"
},
{
"headline": "<a href='#'>2nd headline in content set 1</a>"
},
{
"headline": "<a href='#'>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='#'>1st headline in content set 2</a>"
},
{
"headline": "<a href='#'>2nd headline in content set 2<a/>"
},
{
"headline": "<a href='#'>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='#'>1st headline in content set 3</a>"
},
{
"headline": "<a href='#'>2nd headline in content set 3</a>"
},
{
"headline": "<a href='#'>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='#'>1st headline in content set 4</a>"
},
{
"headline": "<a href='#'>2nd headline in content set 4</a>"
},
{
"headline": "<a href='#'>3rd headline in content set 4</a>"
}
]
}
]
【问题讨论】:
标签: json angularjs angular-strap