搜尋

首頁  >  問答  >  主體

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)

})

這個有問題 為什麼

漂亮男人漂亮男人2826 天前580

全部回覆(1)我來回復

  • 为情所困

    为情所困2017-05-15 17:13:12

    你的input的ngModal改變的時候,myservice不會重跑,因為myservice在頁面是一個差值,這是一個方法,而不是數據,所有你得watch並觸發它。

    $scope.$watch('txtnum', function(val) {
      $scope.myservice = tostring.myfuc($scope.txtnum)
    });

    回覆
    0
  • 取消回覆