angular.js - angular 自定義服務(wù)向方法傳遞參數(shù)問題
問題描述
我自定義了一個服務(wù) 傳入數(shù)字返回字符串的狀態(tài)但是我把輸入框的值傳入寫的好像不對 求帶
<p ng-app='app7' ng-controller='myctrl7'><input type='text' ng-model='txtnum'><p> {{myservice}}</p> </p>var app7 = angular.module(’app7’, []) app7.service(’tostring’, function () { this.myfuc = function (x) {if (x == 1) { return '未開課'} else if (x == 2) { return '已開課'} else if (x == 3) { return '已結(jié)課'} else { return '課程異常'} }})app7.controller(’myctrl7’, function ($scope, tostring) { $scope.myservice = tostring.myfuc($scope.txtnum)})
這個有問題 為什么
問題解答
回答1:你的input的ngModal改變的時候,myservice不會重跑,因為myservice在頁面是一個差值,這是一個方法,而非數(shù)據(jù),所有你得watch并觸發(fā)它。
$scope.$watch(’txtnum’, function(val) { $scope.myservice = tostring.myfuc($scope.txtnum)});
相關(guān)文章:
1. python - linux怎么在每天的凌晨2點執(zhí)行一次這個log.py文件2. 關(guān)于mysql聯(lián)合查詢一對多的顯示結(jié)果問題3. 實現(xiàn)bing搜索工具urlAPI提交4. MySQL主鍵沖突時的更新操作和替換操作在功能上有什么差別(如圖)5. 數(shù)據(jù)庫 - Mysql的存儲過程真的是個坑!求助下面的存儲過程哪里錯啦,實在是找不到哪里的問題了。6. windows誤人子弟啊7. 冒昧問一下,我這php代碼哪里出錯了???8. 如何用筆記本上的apache做微信開發(fā)的服務(wù)器9. 我在網(wǎng)址中輸入localhost/abc.php顯示的是not found是為什么呢?10. mysql優(yōu)化 - MySQL如何為配置表建立索引?
