您的位置:首页技术文章
文章详情页

angular.js - angular 自定义服务向方法传递参数问题

【字号: 日期:2024-09-20 11:41:46浏览:10作者:猪猪

问题描述

我自定义了一个服务 传入数字返回字符串的状态但是我把输入框的值传入写的好像不对 求带

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

angular.js - angular 自定义服务向方法传递参数问题这个有问题 为什么

angular.js - angular 自定义服务向方法传递参数问题

问题解答

回答1:

你的input的ngModal改变的时候,myservice不会重跑,因为myservice在页面是一个差值,这是一个方法,而非数据,所有你得watch并触发它。

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

相关文章: