How can I add some small utility functions to my AngularJS application?

后端 未结 7 1789
情深已故
情深已故 2020-12-02 03:46

I would like to add some utility functions to my AngularJS application. For example:

$scope.isNotString = function (str) {
    return (typeof str !== \"strin         


        
7条回答
  •  醉话见心
    2020-12-02 04:25

    You can also use the constant service as such. Defining the function outside of the constant call allows it to be recursive as well.

    function doSomething( a, b ) {
        return a + b;
    };
    
    angular.module('moduleName',[])
        // Define
        .constant('$doSomething', doSomething)
        // Usage
        .controller( 'SomeController', function( $doSomething ) {
            $scope.added = $doSomething( 100, 200 );
        })
    ;
    

提交回复
热议问题