angular.js - angular custom service passing parameters to methods problem
漂亮男人
漂亮男人 2017-05-15 17:11:12
0
1
583

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

漂亮男人
漂亮男人

reply all(1)
为情所困

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)
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template