2017-04-03 204 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]); 
}