Javascript 检测angularjs中的输入文本长度
我是Angularjs的初学者Javascript 检测angularjs中的输入文本长度,javascript,angularjs,Javascript,Angularjs,我是Angularjs的初学者 <div ng-app> <input type="text" ng-model="Number"/> </div> 如有任何建议,将不胜感激 有很多方法可以做到这一点 1.使用内置指令+模板 然而,这样做不是一个好的做法。最好的方法是使用指令 3.使用指令 diretive可以将某些行为附加到DOM元素;有许多内置指令(ng if,ng show,等等),但创建自定义指令非常常见。下面是一个例子: app.directi
<div ng-app>
<input type="text" ng-model="Number"/>
</div>
如有任何建议,将不胜感激 有很多方法可以做到这一点 1.使用内置指令+模板 然而,这样做不是一个好的做法。最好的方法是使用指令 3.使用指令 diretive可以将某些行为附加到DOM元素;有许多内置指令(
ng if
,ng show
,等等),但创建自定义指令非常常见。下面是一个例子:
app.directive('numberLogic', function(){
return {
restrict: 'A',
scope: {},
template: "<input type='text' ng-model='Number2'/> {{Number2}}",
link: function(scope){
scope.$watch('Number2', function(newValue){
if(newValue.length === 0){
console.log('Second number Empty');
} else {
console.log('Second number Has content');
}
});
}
};
});
app.directive('numberLogic',function(){
返回{
限制:“A”,
作用域:{},
模板:“{Number2}}”,
链接:功能(范围){
范围$watch('Number2',函数(newValue){
if(newValue.length==0){
console.log(“第二个数字为空”);
}否则{
log('第二个数字有内容');
}
});
}
};
});
顺便说一句
我看到您的
ng应用程序
指令为空。别忘了为你的应用程序ng app=“appName”
输入一个模块名,并定义一个具有相同名称的模块angular.module('appName',[])代码>(参见jsbin)。您可以使用ng change
比如说
<input type="text" ng-model="Number"
ng-change="(Number.length>0)?alert('ok'):alert('no')"/>
为什么是长度=0无法键入+有人可以帮助吗
app.controller('AppCtrl', function($scope){
$scope.Number = '';
$scope.$watch('Number', function(newValue){
if(newValue.length === 0){
console.log('Empty');
} else {
console.log('Has content');
}
});
});
app.directive('numberLogic', function(){
return {
restrict: 'A',
scope: {},
template: "<input type='text' ng-model='Number2'/> {{Number2}}",
link: function(scope){
scope.$watch('Number2', function(newValue){
if(newValue.length === 0){
console.log('Second number Empty');
} else {
console.log('Second number Has content');
}
});
}
};
});
<input type="text" ng-model="Number"
ng-change="(Number.length>0)?alert('ok'):alert('no')"/>
<div ng-app="app">
<div ng-controller="test">
<input type="text" ng-model="Number"
ng-change="checkLength()"/>
</div>
</div>
angular.module('app', [])
.controller('test',function($scope){
$scope.checkLength = function(Number){
if(Number.length>0){
//
}
}
})