【发布时间】:2016-07-31 15:22:06
【问题描述】:
这是我第一次在这里问什么,但我完全被卡住了。 我正在用 Angular 创建一个交互式 svg 地图。它适用于 D3,但我想使用 Angular 及其指令和模板。
目前,我设法让地图显示在屏幕上并对其进行操作,但性能很糟糕。 我知道我必须过滤我的数据,但我不知道在哪里:它是在控制器上,还是指令的链接,还是 API?我不知道。
请注意,我仍然是 Angular 的菜鸟(几乎不到一个月),即使我在 3 个月前开始学习代码时也是如此。所以,你肯定会在这里读到一些愚蠢的东西。
无论如何,我的问题是:我应该怎么做才能用简单的 ng-if 替换复杂的 ng-hide(我想根据年份输入删除路径)。
所以,我正在从我的 API 中检索 JSON 路径列表。
我的json: [ {“land_name”:“landa”,“路径”:“...”,“出生”:5000,结束:5152} {“land_name”:“landi”,“路径”:“...”,“出生”:1200,结束:7100} { "land_name" : "lando", "path" : "...", "born" : 100, end : 4000} ]
我的 api(节点 + 猫鼬):
// get all the lands (accessed at GET http://localhost:8080/api/landapi)
.get(function(req, res) {
landapi.find(function(err, docs) {
if (err)
res.send(err);
res.json(docs);
});
});
角度:
我的工厂:
.factory('landDataFactory', ['$http', function($http) {
return $http.get('http://localhost:3000/api/landapi')
.success(function(data) {
return data;
})
.error(function(err) {
return err;
});
我的控制器:
.controller('testCtrl', function($scope, landDataFactory) {
var vm = this;
vm.paysList = [];
vm.year = ''; (==> ng-model in the template)
landDataFactory.success(function(data) {
vm.land = data;
for (var i = 0; i < vm.land.length; i++) {
vm.LandList.push(vm.land[i]);
}
});
我的指令:
.directive('dynamicMap', function() {
return {
restrict: 'E',
replace : true,
scope: {
datapath : '=',
year : '='
},
templateUrl : './modules/dynamicmap.tpl.html',
link : function(scope, element, attrs) {
}
};
我的模板:
<div>
<div class="year">
<input ng-model="year" type="number" step="50" class="inputDate col-xs-2 form-control" placeholder="date""> <br/>
</div>
<svg class="Map">
<g class="drawLand" ng-repeat="data in datapath">
<path ng-hide="year == NULL || data.born > year || data.end < year" ng-attr-d="{{ data.path }}" id="{{ data.id_land }}"></path>
</g>
</svg>
</div>
我调用它时的指令:
<dynamic-map datapath="vm.paysList" year="vm.year"></dynamic-map>
提前致谢
【问题讨论】:
-
您抱怨的性能部分。是启动时的渲染时间还是滚动/平移地图时的渲染时间。或者当范围值发生变化时
-
第 2 次和第 3 次,因为当我滚动范围值时会发生变化
标签: javascript angularjs node.js mongodb svg