2014-05-24 34 views

回答

0

res.render('viewpath', params)可以採取任何類型的數據,包括功能,這可以在視圖中訪問。

// CONTROLLER 
app.get('/my-route', function (req, res, next) { 
    var myFunc = function() { 
     // Do something 
     return "hello world"; 
    }; 

    res.render('my-view', { "myFunc": myFunc }); 
}); 

// VIEW 
p #{myFunc()} 

或者,您可以將該函數作爲屬性添加到res.locals中。

res.locals.myFunc = function() { 
    ... 
}; 

這可以使用相同的方式在視圖中訪問。