angular.js - angular 自定義服務向方法傳遞參數問題
問題描述
我自定義了一個服務 傳入數字返回字符串的狀態但是我把輸入框的值傳入寫的好像不對 求帶
<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 '已結課'} else { return '課程異常'} }})app7.controller(’myctrl7’, function ($scope, tostring) { $scope.myservice = tostring.myfuc($scope.txtnum)})
這個有問題 為什么
問題解答
回答1:你的input的ngModal改變的時候,myservice不會重跑,因為myservice在頁面是一個差值,這是一個方法,而非數據,所有你得watch并觸發它。
$scope.$watch(’txtnum’, function(val) { $scope.myservice = tostring.myfuc($scope.txtnum)});
相關文章:
1. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker-machine添加一個已有的docker主機問題4. css - 求推薦適用于vue2的框架 像bootstrap這種類型的5. Span標簽6. SessionNotFoundException:會話ID為null。調用quit()后使用WebDriver嗎?(硒)7. android新手一枚,android使用httclient獲取服務器端數據失敗,但是用java工程運行就可以成功獲取。8. css - 關于div自適應問題,大家看圖吧,說不清9. redis啟動有問題?10. java - Collections類里的swap函數,源碼為什么要新定義一個final的List型變量l指向傳入的list?
