javascript 使用 angularJS 参数从 div 调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29863382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invoke function from div with angularJS parameter
提问by blue-sky
Below I'm attempting to convert millisecond to javascript date :
下面我试图将毫秒转换为 javascript 日期:
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
<script type="text/javascript">
parseDate({{test}})
</script>
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
}
function parseDate(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
http://jsfiddle.net/U3pVM/15141/
http://jsfiddle.net/U3pVM/15141/
But error Uncaught SyntaxError: Unexpected token {
is thrown on console.
但是Uncaught SyntaxError: Unexpected token {
在控制台上抛出错误。
What is correct method of invoking function from div with angularJS parameter ?
从带有 angularJS 参数的 div 调用函数的正确方法是什么?
采纳答案by Mukund Kumar
use this code : see fiddle
使用此代码:见小提琴
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
function TodoCtrl($scope) {
$scope.test = "1429831430363"
$scope.parseDate=function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[(new Date()).getUTCMonth()] + ' ' + (new Date()).getUTCDate();
}
}
回答by manzapanza
A more elegant solution to format a date is creating a custom filter because is more clean and you can re-use it everywhere:
格式化日期的更优雅的解决方案是创建自定义过滤器,因为它更干净,您可以在任何地方重复使用它:
Filter:
筛选:
myApp.filter('myMillisecondsToUTCDate', [function() {
return function(milliseconds) {
var date = new Date(milliseconds);
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
};
}]);
Controller:
控制器:
myApp.controller('MyCtrl', function($scope, $filter) {
$scope.testDate = 1429831430363;
// Re-Use the filter everywhere
$scope.formattedDate = $filter('myMillisecondsToUTCDate')($scope.testDate);
});
Html:
网址:
<div ng-controller="MyCtrl">
Format a date using a custom filter: {{testDate | myMillisecondsToUTCDate}}<br/>
Formatted date {{formattedDate}}
</div>
回答by Kop4lyf
Modififying your code:
修改你的代码:
We dont need script inside divs as they are still evaluated seperately.
defined parseDate inside angularjs controller, If this is really general, define it as a service/factory and use it everywhere. That is the angular way.
Your test is a string. It doesnot have getUTCMonth function, it should be a number so that it can be converted to a date so that we can use the above function.
我们不需要 div 中的脚本,因为它们仍然是单独评估的。
在 angularjs 控制器中定义了 parseDate,如果这真的很通用,请将其定义为服务/工厂并在任何地方使用它。这就是角度方式。
你的测试是一个字符串。它没有 getUTCMonth 函数,它应该是一个数字,以便可以将其转换为日期,以便我们可以使用上述函数。
following is the modified code, which works:
以下是修改后的代码,它有效:
<div ng-app>
<div ng-controller="TodoCtrl">
<div>
{{parseDate(test)}}
</div>
</div>
</div>
And the controller:
和控制器:
function TodoCtrl($scope) {
$scope.test = 1429831430363;
$scope.parseDate = function(date) {
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
date = new Date(date);
return months[date.getUTCMonth()] + ' ' + date.getUTCDate();
}
}
See this fiddle: http://jsfiddle.net/U3pVM/15146/
看到这个小提琴:http: //jsfiddle.net/U3pVM/15146/
回答by Iso
You can use angular.element
and jqLite.scope
to get the scope object. Remember that angular and your app need to be initialized for this to work, so you'd better call parseDate
in a callback.
您可以使用angular.element
和jqLite.scope
来获取范围对象。请记住,需要初始化 angular 和您的应用程序才能使其工作,因此您最好调用parseDate
回调。
parseDate(
angular
.element('[ng-controller="TodoCtrl"]') // but you'd rather give it an ID / class name / whatever
.scope()
.test
);