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. phpstudy8.1沒集成mysql-front2. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””3. docker鏡像push報錯4. Docker for Mac 創建的dnsmasq容器連不上/不工作的問題5. node.js - mongodb查找子對象的名稱為某個值的對象的方法6. 前端 - @media query 使用出現的問題?7. javascript - QQ第三方登錄的問題8. html5 - datatables 加載不出來數據。9. javascript - 在 model里定義的 引用表模型時,model為undefined。10. docker 下面創建的IMAGE 他們的 ID 一樣?這個是怎么回事????

網公網安備