如何遍历对象数组并重写属性值
How To Loop Over Array Of Objects And Rewrite A Property Value
我有一个包含对象的 Feature 数组。featureUrl 从它从数据库中收集的服务返回。在我使用 ngRoute 的桌面角度应用程序中,一切都很好用。但是,我正在构建一个使用 UIRouter 的 Ionic 移动应用程序,这似乎导致了一个问题。我无法构建如下所示的 url,因为它们都从状态提供程序输出"index.html#/app/my-account"。
离子代码缩写
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'AppCtrl'
})
.state('app.my-account', {
url: '/my-account',
views: {
'menuContent': {
templateUrl: 'templates/my-account.html',
controller: 'accountController'
}
}
})
所以,我的问题是,在我的 Ionic 应用程序中,是否可以直接从尾部斜杠构建 URL?(即 index.html#/my-account)所以它们与我的 Svc 对象匹配,还是它们总是附加到"app"后面,如 index.html#/app/my-account?
如果没有,有人可以帮助我循环我的对象并拆分 Url 字符串并将/app/插入到 url 中,以便我的数据适用于 UIRouter?
Features
[{
featureClass: 'hasStuff',
featureUrl: 'index.html#/my-account',
featureBlah: 'blah'
},
{
featureClass: 'hasCoolStuff',
featureUrl: 'index.html#/my-profile',
featureBlah: 'blah'
},
{
featureClass: 'hasMoreCoolStuff',
featureUrl: 'index.html#/motorcycles',
featureBlah: 'blah'
}]
这是我在重写 featureUrl 字符串时的想法,但遇到了困难。
var featureUrlRewrite = function () {
var index;
var alteredUrl;
for (index = 0, index < $scope.Features.length, index++) {
alteredUrl = $scope.featureUrl[index];
featureUrl = alteredUrl.split("/").append("app/")
}
};
如果您使用正则表达式在 # 符号后面添加"/app",它可能会起作用:
for(var index=0, index<$scope.Features.length(); index++){
$scope.Features[index]['featureUrl'] = $scope.Features[index]['featureUrl'].replace("(.*#)(.*)", "$1/app$2");
}
这应该捕获之前的所有内容,包括 # 符号以及它之后的所有内容,作为单独的捕获并在字符串的两个部分之间插入/app。
我不熟悉ngRoute或您正在使用的平台,但希望对您有所帮助!
相关文章:
- 如何在映射数组中添加换行符
- javascript结合了数组和字典
- 需要帮助设置json数组
- 不能从angular2中的子组件指定父组件中的数组
- 使用JS将数组转换为json对象
- 数组在递归方法中设置为null
- knockoutjs可观察数组
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 如何遍历对象数组并重写属性值
- 如何重写特定数组的方法
- Javascript关联数组:推送一个对象会重写其他数组元素
- 如何防止在循环时重写数组中的数据
- JavaScript重写数组方法
- 在JavaScript中重写数组文字
- Javascript数组或对象可以被重写为可调用的吗?
- 从数组重写链接
- Javascript array.find()回调将对象重写为全局数组
- 重写数组push时调用原始方法
- 如何重写函数.Length在严格模式下返回数组的长度
- 为什么在Javascript中数组重写函数内的所有值?