在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