2016-03-14 80 views
0

我有一個包含對象的特徵數組。 featureUrl從它從數據庫收集的服務中返回。所有在使用ngRoute的桌面角度應用程序中效果很好。但是,我正在構建一個使用UIRouter的Ionic移動應用程序,這似乎是一個問題。我無法構建如下所示的url,因爲它們都從狀態提供程序輸出'index.html#/ app/my-account'。如何在數組對象上循環並重寫屬性值

離子碼Abbrv

.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' 
    } 
} 
}) 

所以,我的問題是,在我的應用程序離子,是可以直接建立網址,關閉尾隨斜線的? (即index.html#/ my-account),以便它們匹配我的Svc對象,或者它們總是會像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/") 
} 
}; 

回答

0

如果使用正則表達式來添加「/應用程序」#符號背後可能工作:

for(var index=0, index<$scope.Features.length(); index++){ 
    $scope.Features[index]['featureUrl'] = $scope.Features[index]['featureUrl'].replace("(.*#)(.*)", "$1/app$2"); 
} 

這應該抓住一切前和後加上#符號以及一切的在字符串的兩個部分之間分開捕獲和插入/應用。

我不熟悉ngRoute或您正在使用的平臺,但希望能幫到您!

+0

'replace'不會修改原來的...它會返回一個新的字符串 – charlietfl

+0

只需注意到我需要添加註釋以通知用戶更改...以上內容是否解決您的問題? –