33

In controller I have followed methods:

var isPaused  = false; 

$scope.switcher = function (booleanExpr, trueValue, falseValue) {
    return booleanExpr ? trueValue : falseValue;
};

$scope.isPaused = function () {
    return isPaused;
};

And I can call it from HTML like:

<body  ng-controller="Cntrl">

...
<h4>
 {{ switcher( isPaused(), 'Search Address Mode', 'Search Location Mode' )}}
</h4>
 <div class="btn-group">
    ...     
 </div>

As you see if isPaused() returns false I get <h4>Search Location Mode</h4>

This is utility therefore I want to define it as factory

feederliteModule.factory('switcher', function () {   
return {
    sw: function (booleanExpr, trueValue, falseValue) {
        return booleanExpr ? trueValue : falseValue;  
    }
  };
});

No exceptions but

when I try to call it like:

<h4>
 {{ switcher.sw( isPaused(), 'Search Address Mode', 'Search Location Mode' )}}
</h4>
 <div class="btn-group">
    ...     
 </div>

Nothing happens.

**I added 'switcher' to controller.

How can I call factory method from HTML?

(*You welcome to change/edit my question if it seems not clear)

Thank you,

5
  • how did you manage to accomplish this?I have a similar problem … might be I just shouldn't be doing all this like that Commented Feb 11, 2014 at 13:29
  • @user1524316 well, see answer below Commented Feb 11, 2014 at 13:36
  • 1
    I thought it did not solve your question entirely since its not accepted? Commented Feb 11, 2014 at 13:41
  • couse I know you can call it like he showed below. But I need to be able to call it with ng-click … so I just need to leave my method in controller and not in a separate factory? Commented Feb 11, 2014 at 13:43
  • @user1524316 yep, it was some gap, accepted Commented Feb 11, 2014 at 14:28

3 Answers 3

52

Well, you're not really supposed to do that... but what you can do is put your service object in a property on your $scope, and call it from there.

app.controller('MyCtrl', function($scope, switcher) {
   $scope.switcher = switcher;
});
Sign up to request clarification or add additional context in comments.

4 Comments

Why not supposed? is it seems messy?. Your solution works. THe question is why I need create $scope.switcher if controller receives switcher identifier. Thanks
HTML views can only be linked to Controller (using the $scope variable) or extended with Directives. Any other way is not an angular way :)
@Chandermani I use this method in several controllers and the easy/short way is to create factory or service.
Because when you add {{}} expressions, angular calls $scope.$eval() and $scope.$eval looks at things differently, it seems MyVariable as $scope.MyVariable, if you try {{switcher}} it will just look for $scope.switcher.
3

I had a more general question, "How to call angularjs scope/factory/service from html on page load." My solution was to call the method within ng-show.

  1. At the highest element surrounding the context in question,
  2. Call the method using the ng-show directive.
  3. The method will run prior to displaying the page.
  4. Note you can also use a solution like this to make sure that bootstrapping work is finished prior to displaying a page (this can reduce the number of moving parts in a scattered rendering)

<div ng-show="runThisMethod(someVarOnScope)" .....>

Comments

0

Using AngularJS 1.7.2 and Page Controller (Every Webpage has it's own controller)

First take the Factory view:

AppName.factory('eventMate', function() {
    let obj = this;

    obj.search = "";

    obj.setSearchValue = function(value) {
        obj.search = value;
        return obj.search;
    };

    obj.getSearchValue = function() {
        return obj.search;
    };

    return obj;
});

In one of the child Controller, named rfcController

AppName.controller("rfcController", function ($scope, $http, $filter, $window, $location, $anchorScroll,
                                               textMate, datetimeMate, restpack, pagination, imageMate, eventMate) {

    //there are many more service providers and injectors also, just showing for an idea
    $scope.eventMate = eventMate;


    $scope.getFiltered = function(){
        let searchValue = $scope.eventMate.getSearchValue().toString();

        if(searchValue === undefined){
            return "";
        } else {
            return searchValue.toString();
        }
    };
}

Now see the source through html side, where I will call this method.

<table>
    <tr class="rfc_table_td_tr" ng-repeat="(key, value) in rfcDetails | filter:getFiltered()">
        <td>{{value.createdDate}}</td>
        <td>{{value.rfcTitle}}</td>
        <td>{{value.rfcDetails}}</td>
    </tr>
</table>

Hope it will help many one. :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.