angular.js - angular 自定義服務(wù)向方法傳遞參數(shù)問(wèn)題
問(wèn)題描述
我自定義了一個(gè)服務(wù) 傳入數(shù)字返回字符串的狀態(tài)但是我把輸入框的值傳入寫(xiě)的好像不對(duì) 求帶
<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 '未開(kāi)課'} else if (x == 2) { return '已開(kāi)課'} else if (x == 3) { return '已結(jié)課'} else { return '課程異常'} }})app7.controller(’myctrl7’, function ($scope, tostring) { $scope.myservice = tostring.myfuc($scope.txtnum)})
這個(gè)有問(wèn)題 為什么
問(wèn)題解答
回答1:你的input的ngModal改變的時(shí)候,myservice不會(huì)重跑,因?yàn)閙yservice在頁(yè)面是一個(gè)差值,這是一個(gè)方法,而非數(shù)據(jù),所有你得watch并觸發(fā)它。
$scope.$watch(’txtnum’, function(val) { $scope.myservice = tostring.myfuc($scope.txtnum)});
相關(guān)文章:
1. python文檔怎么查看?2. javascript - 有適合開(kāi)發(fā)手機(jī)端Html5網(wǎng)頁(yè)小游戲的前端框架嗎?3. python - Pycharm的Debug用不了4. html - eclipse 標(biāo)簽錯(cuò)誤5. datetime - Python如何獲取當(dāng)前時(shí)間6. javascript - 關(guān)于apply()與call()的問(wèn)題7. python - pycharm 自動(dòng)刪除行尾空格8. python - pandas按照列A和列B分組,將列C求平均數(shù),怎樣才能生成一個(gè)列A,B,C的dataframe9. 安全性測(cè)試 - nodejs中如何防m(xù)ySQL注入10. python 利用subprocess庫(kù)調(diào)用mplayer時(shí)發(fā)生錯(cuò)誤
