简体   繁体   English

AngularJS-从未使用ng-submit调用的函数

[英]AngularJS - function never called with ng-submit

I've created an angular-promise which calls a http method (corresponding to a rest service),which is supposed to be called when a form is submitted, using ng-submit. 我创建了一个有角度的承诺,该承诺会调用http方法(对应于rest服务),该方法应该在使用ng-submit提交表单时被调用。 It never happens, and there is no error, it seems like the function is never called. 它永远不会发生,并且没有错误,似乎从未调用过该函数。 So here is the javascript code: 因此,这是JavaScript代码:

myapp.factory('CardFactory', ['$http', function($http){
return{
    cardService: function(hclass) {
        return $http({
            method: 'get',
            url: UrlServices.baseUrl + 'http://localhost:8080//HSRestServices/hsrest/decks/getCards/' + hclass,
        })
    }
    }
  }])
myapp.controller('CardCtrl', ['$scope',  'CardFactory', function($scope, CardFactory ){

$scope.card = "Druid";

$scope.cardService = function() {


    CardFactory.cardService($scope.card)
        .then(function (response) {

            $scope.status = response.status;
            $scope.card = response.data;

            console.log("Response: " + JSON.stringify({data: response.data}));

            if (response.status == 200){
                $scope.card = response.data;
            } else {
                console.log("Response: Something went wrong.");
            }
        }, function (response) {
            console.log("Response: Something went wrong.");
        })
};
 }]);

and the html code: 和html代码:

<body ng-app="mainApp">
<div ng-controller="CardCtrl">
<form ng-submit="cardService()">
    <input type="submit" value="Submit">
</form>
<p ng-model="card">{{card}}</p>

  </div>

 </body>

Any ideas?Thank you in advance. 有什么想法吗?谢谢。

If you don't see the word "Druid" on the app, most likely you haven't included angular.js and app.js in your application. 如果您在应用程序中没有看到“ Druid”一词,则很可能您的应用程序中没有包含angular.js和app.js。

If you do, it's probably working, and you haven't properly defined UrlServices, and it cannot find it. 如果这样做,它可能正在工作,并且您没有正确定义UrlServices,并且找不到它。 You can check the browser console for more details. 您可以检查浏览器控制台以了解更多详细信息。

Otherwise it works for me. 否则对我有用。 Check out this jsfiddle: https://jsfiddle.net/j8o0ag4t/ 看看这个jsfiddle: https ://jsfiddle.net/j8o0ag4t/

In the console I see: 在控制台中,我看到:

 Error: Can't find variable: UrlServices

cardService@ http://fiddle.jshell.net/j8o0ag4t/show/:50:29 cardService@ http://fiddle.jshell.net/j8o0ag4t/show/:62:28 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:160:97 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:177:84 $eval@ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:100:328 $apply@ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:101:110 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:177:71 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:27:19 forEach@[native code] p@ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:7:262 c@ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:26:493 cardService @ http://fiddle.jshell.net/j8o0ag4t/show/:50:29 cardService @ http://fiddle.jshell.net/j8o0ag4t/show/:62:28 http://ajax.googleapis.com/ ajax / libs / angularjs / 1.2.1 / angular.min.js:160:97 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:177:84 $ eval @ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:100:328 $ apply @ http://ajax.googleapis.com/ajax/libs/angularjs/1.2 .1 / angular.min.js:101:110 http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:177:71 http://ajax.googleapis.com /ajax/libs/angularjs/1.2.1/angular.min.js:27:19 forEach @ [native code] p @ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular。 min.js:7:262 c @ http://ajax.googleapis.com/ajax/libs/angularjs/1.2.1/angular.min.js:26:493

You have to give a name attribute to your form and all its subsequent form elements should have a name themselves too. 您必须为表单赋予name属性,其所有后续表单元素本身也应具有name

Also, you cannot have ng-model on a p tag. 另外,您不能在p标签上使用ng-model Use an input type text as follows: 使用输入类型的文本,如下所示:

HTML: HTML:

<form name="cardForm" ng-submit="cardService()" novalidate>
    <input type="text" name="card" />
    <input ng-model="card" type="submit" value="Submit">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM