AngularJS计算ng模型
我有以下控制器AngularJS计算ng模型,angularjs,Angularjs,我有以下控制器 app.controller("testCtrl", function(){ $scope.utcTime = 1380150771; $scope.parseTime = function(t){ //return local time string } }); 在我看来,我有 <input type="text" ng-model="parseTime(utcTime)" /> 它不起作用。我可以将ng模型绑定到返回字
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.parseTime = function(t){
//return local time string
}
});
在我看来,我有
<input type="text" ng-model="parseTime(utcTime)" />
它不起作用。我可以将ng模型绑定到返回字符串的方法吗?
在输入按钮中显示值的任何替代方法?您可以同时使用ngChange和ngModel JS HTML
ng模型通过标记和控制器进行映射
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.parseTime = function(t){
//return local time string
}
});
首先,您可以看到在输入标记中指定的默认utcTime(1380150771)
当您更改输入标记中的文本时,ng模型(utcTime)将在控制器中自动更改
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.parseTime = function(t){
//return local time string
}
});
然后,您键入的每个字母都将调用ng change(parseTime)函数
您可以通过console.log方法进行检查。我的解决方案基于以下来源: 我已经编辑了Plunker示例代码,以使用
ng值
生成和更新ng模型
,并使用计算功能。请参见以下链接:
此外,上面的示例还显示了如何使用自定义过滤器格式化显示结果
在其他帖子中,一些人建议使用$watch()
来检测输入变量的变化,并相应地更新ng model
变量。使用ng value
比使用$watch()
要好得多,因为后者迫使您在手表中包含所有输入变量,如果您有非常复杂的计算模型,这可能是不可能的
塔瑞克试着这样做
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.result= $scope.parseTime( $scope.utcTime)
$scope.parseTime = function(t){
//return local time string
}
});
html
是,您可以尝试以下操作:
其工作实例:
<input type="text" ng-model="parseTime(utcTime)" />
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.parseTime = function(t){
//return local time string
new Date(t).toISOString();
}
});
app.controller(“testCtrl”,function()){
$scope.utcTime=1380150771;
$scope.parseTime=函数(t){
//返回本地时间字符串
新日期(t).toISOString();
}
});
我不相信您可以将ng模型绑定到函数,您只是想将初始值设置为硬编码的utcTime吗?您到底想实现什么?我需要显示值。我什么都不装订。此外,我还需要在用户设置日期时检测值的变化
<input type="text" ng-model="parseTime(utcTime)" />
app.controller("testCtrl", function(){
$scope.utcTime = 1380150771;
$scope.parseTime = function(t){
//return local time string
new Date(t).toISOString();
}
});