如何调用从HTML中angularjs工厂方法? [英] How to call factory methods from HTML in angularjs?

查看:160
本文介绍了如何调用从HTML中angularjs工厂方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

控制器我按照方法:

var isPaused  = false; 

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

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

我可以从HTML一样调用它:

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>

正如你看看 isPaused得到()收益我得到&LT; H4&GT;搜索定位模式和LT; / H4&GT;

这是工具,因此我想将它定义为工厂

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;  
    }
  };
});

没有异常,但

当我尝试调用它像:

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

什么也没有发生。

Nothing happens.

**我添加'切换'来控制。

我如何可以调用工厂方法从HTML?

How can I call factory method from HTML?

(*欢迎您到更改/编辑我的问题,如果它似乎不明确)

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

感谢您,

推荐答案

好了,你没有真正应该做的......但你的可以的做的就是把你的服务在对象在你的$ scope属性,并从那里调用它。

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;
});

这篇关于如何调用从HTML中angularjs工厂方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆