2017-04-03 184 views
0

回調之前濺這是我的一段代碼:我怎樣才能在AngularJS

.factory('Latest', function(EBSheadless) { 
    return { 
    posts: function() { 
     // Call the API, and define the specific endpoint 
     return EBSheadlessDrupalAPI.loadEndpoint('views/news.json'); 
    }, 
    post: function(posts,id,callback){ 
     var findpost = {}; 
     for(var i=0;i<posts.length;i++) { 
     findpost[posts[i].nid] = posts[i]; 
     } 
     callback(findpost[id]); 
    } 
    }; 
}) 

它得到正確的結果,例如:

this-is-a-news 

但我需要這個鏈接是這樣的(我不能編輯後端):

/this-is-a-news 

所以我必須在我的回調中添加/。我嘗試了很多職位,但沒有成功。 我該如何解決它?謝謝!

回答

0

爲什麼你不只是像這樣連接字符串callback("/"+findpost[id]);。這將返回/this-is-a-news

post: function(posts,id,callback){ 
     var findpost = {}; 
     for(var i=0;i<posts.length;i++) { 
     findpost[posts[i].nid] = posts[i]; 
     } 
     callback("/"+findpost[id]); 
}