Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call factory methods from HTML in angularjs?

Tags:

angularjs

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,

like image 353
Maxim Shoustin Avatar asked May 14 '13 14:05

Maxim Shoustin


People also ask

What is the factory method in AngularJS?

What is Factory in AngularJS? Factory is an angular function which is used to return the values. A value on demand is created by the factory, whenever a service or controller needs it. Once the value is created, it is reused for all services and controllers. We can use the factory to create a service.

What is $resource in AngularJS?

$resource documentation describes it as: A factory which creates a resource object that lets you interact with RESTful server-side data sources. $resource is most powerful when it's configured with a classic RESTful backend.

What is factory and service in AngularJS?

factory() is a method that takes a name and function that are injected in the same way as in service. The major difference between an AngularJS service and an AngularJS factory is that a service is a constructor function and a factory is not.

What is factory method in AngularJS Mcq?

AngularJS Factory Method makes the development process of AngularJS application more robust. A factory is a simple function which allows us to add some logic to a created object and return the created object.


2 Answers

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; }); 
like image 99
Ben Lesh Avatar answered Oct 07 '22 22:10

Ben Lesh


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)" .....>

like image 28
Robert Christian Avatar answered Oct 07 '22 23:10

Robert Christian