search

Home  >  Q&A  >  body text

angular.js - angular custom service passing parameters to methods problem

I have customized a service that returns the status of a string by passing in numbers
But the value I passed into the input box does not seem to be written correctly. Please help

 <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)

})

This is problematic. Why

漂亮男人漂亮男人2865 days ago595

reply all(1)I'll reply

  • 为情所困

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

    When the ngModal of your input changes, myservice will not re-run, because myservice is a difference value on the page. This is a method, not data, so you have to watch and trigger it.

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

    reply
    0
  • Cancelreply