Using helper methods while templating with Angular JS

后端 未结 3 609
粉色の甜心
粉色の甜心 2020-12-15 19:57

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 method

3条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-15 20:34

    Looking at the presented use case your best call would be the date filter described here: http://docs.angularjs.org/api/ng.filter:date Using this filter you could write:

    {{CreatedDate | date}}
    

    The mentioned filter is customizable so you could use different date formats etc.

    Generally speaking filters are very nice for encapsulating formatting logic / UI helper functions. More on creating filters here: http://docs.angularjs.org/guide/dev_guide.templates.filters.creating_filters

    Filters are nice and fit many use cases but if you are simply after using any function in your template it is possible. Just define a function in a scope and you are ready to use it directly in your template:

    {{doSomething(CreatedDate)}}
    

    where doSomething needs to be defined on a scope (a current one or one of the parent scopes):

    function MyCtrl($scope) {
    
        $scope.doSomthing = function(argument){
            //ui helper logic here
        }    
    }
    

提交回复
热议问题