如何在angularjs中从HTML调用工厂方法?

时间:2022-09-24 23:13:30

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:

我可以把它叫做HTML

<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>

如果isPaused()返回false,则会得到

搜索位置模式

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?

如何从HTML调用factory方法?

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

(*如果我的问题不清楚,你可以修改/修改)

Thank you,

谢谢你!

2 个解决方案

#1


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.

你不应该这么做……但是,您可以做的是将服务对象放入您的$scope的属性中,并从那里调用它。

app.controller('MyCtrl', function($scope, switcher) {
   $scope.switcher = switcher;
});

#2


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.

我有一个更一般的问题,“如何在页面加载时从html调用angularjs范围/工厂/服务。”我的解决方案是在ng-show中调用方法。

  1. At the highest element surrounding the context in question,
  2. 在有关上下文的最高元素中,
  3. Call the method using the ng-show directive.
  4. 使用ng-show指令调用该方法。
  5. The method will run prior to displaying the page.
  6. 该方法将在显示页面之前运行。
  7. 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)
  8. 注意,您还可以使用这样的解决方案来确保在显示页面之前完成引导工作(这可以减少分散呈现中移动部件的数量)

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

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

#1


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.

你不应该这么做……但是,您可以做的是将服务对象放入您的$scope的属性中,并从那里调用它。

app.controller('MyCtrl', function($scope, switcher) {
   $scope.switcher = switcher;
});

#2


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.

我有一个更一般的问题,“如何在页面加载时从html调用angularjs范围/工厂/服务。”我的解决方案是在ng-show中调用方法。

  1. At the highest element surrounding the context in question,
  2. 在有关上下文的最高元素中,
  3. Call the method using the ng-show directive.
  4. 使用ng-show指令调用该方法。
  5. The method will run prior to displaying the page.
  6. 该方法将在显示页面之前运行。
  7. 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)
  8. 注意,您还可以使用这样的解决方案来确保在显示页面之前完成引导工作(这可以减少分散呈现中移动部件的数量)

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

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