gpt4 book ai didi

angularjs - AngularJS Controller 中的函数未被调用

转载 作者:行者123 更新时间:2023-12-01 01:58:08 25 4
gpt4 key购买 nike

我正在尝试使用以下代码从我的 AngularJS Controller 调用一个函数:

Controller 代码

(function () {

var hello = angular.module("helloApp", []);

hello.controller("HelloCtrl", ["$http", "$scope", function($scope, $http) {
console.log("in controller");
$scope.test = function() {
console.log("in test function");
$http.get("/test").success(function(data) {
console.log("success");
})
.error(function(data) {
console.log("error");
});
};
} ]);

})();

HTML

<!DOCTYPE html>
<html ng-app="helloApp">
<head>
...
</head>
<body ng-controller="HelloCtrl">
<div class="container">
<form class="form-horizontal">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default" ng-click="test()">Search</button>
</div>
</div>
</form>
</div>
...
</body>

当我启动应用程序时,我看到消息 "in controller",正如我所期望的那样。但是,如果我单击提交按钮,则我看不到任何其他三个控制台消息。

我错过了什么?

最佳答案

你的依赖注入(inject)顺序是错误的。

hello.controller("HelloCtrl", ["$http", "$scope", function($scope, $http) {

应该是

 hello.controller("HelloCtrl", ["$http", "$scope", function($http, $scope) {

参数的顺序必须与名称数组的顺序相同。

关于angularjs - AngularJS Controller 中的函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28822591/

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