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. nignx - docker內nginx 80端口被占用2. java - SSH框架中寫分頁時service層中不能注入分頁類3. angular.js - angular內容過長展開收起效果4. docker鏡像push報錯5. python3.x - python連oanda的模擬交易api獲取json問題第五問6. docker-machine添加一個已有的docker主機問題7. node.js - 我是一個做前端的,求教如何學習vue,node等js引擎?8. html5 - 百度echart官網下載的地圖json數據亂碼9. debian - docker依賴的aufs-tools源碼哪里可以找到啊?10. 關于docker下的nginx壓力測試
