使用辅助方法,而用角JS模板化 [英] Using helper methods while templating with Angular JS
本文介绍了使用辅助方法,而用角JS模板化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
目前从其previous模板变换一个网站,以角的过程。在previous模板过程中,我们用我们能够调用辅助方法才能正确显示数据。例如:
Currently in the process of converting a website from its previous templating to Angular. In the previous templating process we were using we were able to call helper methods to display data correctly. For instance:
<script type="text/javascript">
$.views.helpers({
parseDate: function (jsonDate) {
if (jsonDate != null) {
var newDate = Utils.PrettyDate(Utils.ConvertJsonDateToJsDate(jsonDate));
return newDate;
}
}
});
</script>
<div class="post-info">
<span class="posted-date">Posted {{:~parseDate(CreatedDate)}}</span> | <span>{{:ReplyCount}} Replies</span>
</div>
这是非常好的。试图找出一种方法来尽可能去模板利用具有角相同类型的功能。是否有可能做同样的事情?如果是的话怎么样?
This was very nice. Trying to figure out a way to utilize the same type of functionality with Angular as far as templating goes. Is it possible to do something similar? If so how?
推荐答案
您只需将方法添加到您的控制器。事情是这样的:
You simply add the method to your controller. Something like this:
<div class="post-info" ng-controller="MyCtrl">
<span class="posted-date">Posted {{parseDate(CreatedDate)}}</span>
</div>
然后,控制器:
function MyCtrl($scope)
{
$scope.parseDate = function(jsonDate) {
//date parsing functionality
return newParsedDate;
}
}
这篇关于使用辅助方法,而用角JS模板化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文