angular.js - angular 自定義服務(wù)向方法傳遞參數(shù)問題
問題描述
我自定義了一個(gè)服務(wù) 傳入數(shù)字返回字符串的狀態(tài)但是我把輸入框的值傳入寫的好像不對(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 '未開課'} 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)})
這個(gè)有問題 為什么
問題解答
回答1:你的input的ngModal改變的時(shí)候,myservice不會(huì)重跑,因?yàn)閙yservice在頁面是一個(gè)差值,這是一個(gè)方法,而非數(shù)據(jù),所有你得watch并觸發(fā)它。
$scope.$watch(’txtnum’, function(val) { $scope.myservice = tostring.myfuc($scope.txtnum)});
相關(guān)文章:
1. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?2. mysql - 把一個(gè)表中的數(shù)據(jù)count更新到另一個(gè)表里?3. 請(qǐng)教使用PDO連接MSSQL數(shù)據(jù)庫(kù)插入是亂碼問題?4. python - 爬蟲模擬登錄后,爬取csdn后臺(tái)文章列表遇到的問題5. visual-studio - Python OpenCV: 奇怪的自動(dòng)補(bǔ)全問題6. linux - Ubuntu下編譯Vim8(+python)無數(shù)次編譯失敗7. node.js - nodejs開發(fā)中常用的連接mysql的庫(kù)8. mysql 查詢身份證號(hào)字段值有效的數(shù)據(jù)9. 視頻文件不能播放,怎么辦?10. mysql - 分庫(kù)分表、分區(qū)、讀寫分離 這些都是用在什么場(chǎng)景下 ,會(huì)帶來哪些效率或者其他方面的好處
