在controller
我按照方法:如何在angularjs中从HTML中调用工厂方法?
var isPaused = false;
$scope.switcher = function (booleanExpr, trueValue, falseValue) {
return booleanExpr ? trueValue : falseValue;
};
$scope.isPaused = function() {
return isPaused;
};
我可以从HTML喜欢叫它:
<body ng-controller="Cntrl">
...
<h4>
{{ switcher(isPaused(), 'Search Address Mode', 'Search Location Mode')}}
</h4>
<div class="btn-group">
...
</div>
正如你看到的,如果isPaused()
回报false
我得到<h4>Search Location Mode</h4>
这是实用工具,因此我想将其定义为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>
什么也没有发生。
**我向控制器添加了'switcher'
。
如何从HTML调用工厂方法?
(*欢迎你改变/编辑我的问题,如果它似乎不明确)
谢谢
你是怎么做到的?我也有类似的问题......可能是我不应该这样做, – trainoasis 2014-02-11 13:29:34
@ user1524316好吧,看下面的回答 – 2014-02-11 13:36:09
我以为它并没有完全解决你的问题,因为它不接受? – trainoasis 2014-02-11 13:41:22