【发布时间】:2017-02-13 17:25:42
【问题描述】:
我是角度的初学者。 请帮我修复this plunk
我试图只对我的控制器中的 jquery UI datepicker 小部件的 onSelect 回调使用一个回调,而不是在每个指令中重复回调函数(是的,为了实验,我确实有多个指令)。
但是我得到了这个错误
未捕获的 TypeError:无法使用 'in' 运算符在 19/10/2016 中搜索 'onSelect'
这是我的代码
HTML
<html ng-app="myApp">
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css"/>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.js"></script>
<script src="script.js"></script>
</head>
<body ng-controller="myController">
<input type="text" my-datepicker ng-model="date" date="date" on-select="onSelect()"/>
<input type="button" ng-click="submitDate()" value="Submit"/>
</body>
</html>
JS
var app = angular.module('myApp', []);
app.controller('myController', ['$scope', function($scope){
$scope.date = "frommyController";
$scope.submitDate = function(){
console.log($scope.date);
};
$scope.onSelect = function(value, picker){
scope.date = value;
scope.$parent.$digest();
}
}]);
app.directive('myDatepicker', function(){
return {
scope: {
date: "=",
onSubmit: "&onSelect"
},
restrict: 'EA',
require: "ngModel",
link: function(scope, element, attributes, modelController){
scope.date = "fromdirevtive";
element.datepicker({
changeMonth: true,
changeYear: true,
dateFormat: "dd/mm/yy",
onSelect: scope.onSubmit
});
}
}
})
谁能帮我理解我在这里做错了什么?
【问题讨论】:
-
别忘了欣赏我的作品。
-
@chiragpatel 感谢您的帮助 chirag。我明白,因为这个回调应该是从外部环境而不是角度调用的。这是无法实现的。
标签: javascript angularjs angularjs-directive jquery-ui-datepicker