AngularJS |从文件夹建立路径 [英] AngularJS | Build a path from folders
问题描述
我当前正在递归浏览文件夹,并且我想构建"文件夹的路径.
I am currently browsing folders recursively and I want to "build" the path of my folders.
现在,我可以使用以下方式创建路径: folder1/folder2/folder3
,其中包含我要更新的String变量:
Now I can create the path like this: folder1/folder2/folder3
with a String variable that I update like this:
path += nameOfFolder + "/";
但是当我回到 folder2
时,我的路径类似于: folder1/folder2/folder3/folder2
.
but when I go back to folder2
my path is like: folder1/folder2/folder3/folder2
.
我只想删除列表中的最后一个对象而不添加它.
I just want to remove the last object of my list and not adding it.
AngularJS中是否有某些功能可以自动完成所有此系统,还是我必须手动进行?
Are there something in AngularJS to do automatically all this system, or do I have to make that manually?
推荐答案
我仍然不知道如何在AngularJS中使用文件夹.但是,假设您已经可以在它们之间进行导航,建议您使用 stack 来跟踪您访问过的文件夹.堆栈只是适用于主体 Last-in-First-out 的数组.我对分页做了一个小的模拟:
I still don't know how are you using folders in AngularJS. But assuming you already have a way for navigating between them, I suggest using a stack to keep the track of which folders you have visited. A stack is just an array that works on a principal Last-in-First-out. I made this small simulation for pagination:
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
$scope.stack = [];
$scope.path = "/";
$scope.go_to = function(name) {
if (!name || (name == $scope.stack[$scope.stack.length - 1])) {
return;
}
var index = $scope.stack.indexOf(name)
if (index >= 0) { // found such folder
$scope.stack = $scope.stack.slice(0, index + 1);
} else {
$scope.stack.push(name);
}
$scope.path = "/" + $scope.stack.join("/");
}
$scope.go_back = function() {
$scope.stack.pop();
$scope.path = "/" + $scope.stack.join("/");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
<h3>Folders simulation</h3>
Stack: <code>{{stack}}</code>
<br>
Path: <code>{{path}}</code>
<hr>
<button ng-click="go_to(folder_name)">Go to:</button> <input type="text" ng-model="folder_name" /><br>
<button ng-click="go_back()">Go back</button>
</div>
这篇关于AngularJS |从文件夹建立路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!