gpt4 book ai didi

javascript - 添加指令使 Controller 在 Angular js 代码中未定义

转载 作者:行者123 更新时间:2023-11-29 16:15:14 26 4
gpt4 key购买 nike

这是我的带有 html 代码的 angular js 应用

<html>
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/angular.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
angular.module("demo", []).controller('DemoController', function ($scope) {
$scope.user = {
dateOfBirth: new Date(1970, 0, 1)
}
});
</script>
</head>
<body>
<div ng-app="demo" ng-controller="DemoController">
Date Of Birth:
<my-datepicker type="text" ng-model="user.dateOfBirth" />
<br />
Current user's date of birth: <span id="dateOfBirthDisplay">{{user.dateOfBirth}}</span>
</div>
</body>
</html>

它工作正常。但是当我向它添加指令时,它显示错误

Error: Argument 'DemoController' is not a function, got undefined

这里是完整的指令代码

<html>
<head>
<title></title>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/angular.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
angular.module("demo", []).controller('DemoController', function ($scope) {
$scope.user = {
dateOfBirth: new Date(1970, 0, 1)
}
});
angular.module("demo", []).directive('myDatepicker', function ($parse) {
return {
restrict: "E",
replace: true,
transclude: false,
compile: function (element, attrs) {
var modelAccessor = $parse(attrs.ngModel);

var html = "<input type='text' id='" + attrs.id + "' >" +
"</input>";

var newElem = $(html);
element.replaceWith(newElem);

return function (scope, element, attrs, controller) {

var processChange = function () {
var date = new Date(element.datepicker("getDate"));

scope.$apply(function (scope) {
// Change bound variable
modelAccessor.assign(scope, date);
});
};

element.datepicker({
inline: true,
onClose: processChange,
onSelect: processChange
});

scope.$watch(modelAccessor, function (val) {
var date = new Date(val);
element.datepicker("setDate", date);
});

};

}
};
});
</script>
</head>
<body>
<div ng-app="demo" ng-controller="DemoController">
Date Of Birth:
<my-datepicker type="text" ng-model="user.dateOfBirth" />
<br />
Current user's date of birth: <span id="dateOfBirthDisplay">{{user.dateOfBirth}}</span>
</div>
</body>
</html>

我正在按照此链接 http://henriquat.re/directives/advanced-directives-combining-angular-with-existing-components-and-jquery/angularAndJquery.html 中的教程进行操作

最佳答案

您正在定义 demo 模块两次。通过将第二个参数传递给 angular.module method ,您创建一个新模块。不要传递第二个参数以返回对现有模块的引用:

// Create a module
angular.module("demo", []).controller('DemoController', function ($scope) {
// ...
});
// Get reference to module
angular.module("demo").directive('myDatepicker', function ($parse) {
// ^ Only one argument
});

请注意,controller 方法返回允许链接的模块,module 方法显然也返回模块,因此您还有另外两个选择:

链接:

angular.module("demo", []).controller('DemoController', function ($scope) {
// ...
}).directive('myDatepicker', function ($parse) {
// ...
});

存储对模块的引用:

var demo = angular.module("demo", []);
demo.controller('DemoController', function ($scope) {
// ...
});
demo.directive('myDatepicker', function ($parse) {
// ...
});

关于javascript - 添加指令使 Controller 在 Angular js 代码中未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17338197/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com