Javascript 角度服务函数为';t处理用户输入
我正在尝试学习如何创建angular服务,所以我已经创建了一个,但它只在页面加载时起作用,并提供输入值而不是函数结果 HTML:Javascript 角度服务函数为';t处理用户输入,javascript,angularjs,Javascript,Angularjs,我正在尝试学习如何创建angular服务,所以我已经创建了一个,但它只在页面加载时起作用,并提供输入值而不是函数结果 HTML: <input type="text" ng-model="hexVal"> <p>Hex service: {{hex(hexVal)}}</p> 服务使用情况: $scope.hexVal = 255; $scope.hex = function(arg){ return hexafy.myFunc(arg); };
<input type="text" ng-model="hexVal">
<p>Hex service: {{hex(hexVal)}}</p>
服务使用情况:
$scope.hexVal = 255;
$scope.hex = function(arg){
return hexafy.myFunc(arg);
};
需要将输入文本解析为数字:
app.service('hexafy', function() {
this.myFunc = function (x) {
return parseInt(x).toString(16);
};
});
演示
角度模块(“应用程序”,[])
.service('hexafy',function(){
this.myFunc=函数(x){
返回parseInt(x).toString(16);
};
})
.controller('ctrl',函数($scope,hexafy){
$scope.hexVal=255;
$scope.hex=函数(arg){
返回hexafy.myFunc(arg);
};
})
十六进制服务:{Hex(hexVal)}
app.service('hexafy', function() {
this.myFunc = function (x) {
return parseInt(x).toString(16);
};
});