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)});
相關文章:
