简体   繁体   中英

ng-click and ng-submit doesn't work

i have a angular application with phonegap where i have a login form and login controller.

The problem is that the ng-submit doesn't work. Normaly the submit call the fonction formConnexion() but nothing happens. So, i tried with just a alert, but it's the same result...

After i tried with a ng-click and same result. Then, i wanted try to call a sample variable in the scope in the template ($scope.test) and it doesn't display. And the console.log('salut!!') doesn't dispaly when i am on the login page.

Which made me think that my controller it doesn't associate to the template. But i have the ng-controller in my div with the good name controller.

Then, i thought that angular it's was worse installing but no because the other directives work (ng-if, ng-src, ...)

So, if you have a idea to solve this problem, I do not mind :)

here the code for the template :

login.html

<div class="row jumbotron" style="margin:10px" ng-controller="LoginCtrl">
    <div class="col-lg-8 col-lg-offset-2 col-md-8 col-md-offset-2 col-sm-8 col-sm-offset-2 col-xs-8 col-xs-offset-2">
        <h1>{{ test }}</h1>
        <form class="form-horizontal" ng-submit="alert('alert')">
            <div class="form-group">
                <label for="login" class="control-label">Login</label>
                <input type="text" name="login" class="form-control" ng-model="formData.login">
            </div>
            <div class="form-group">
                <label for="password" class="control-label">Password</label>
                <input type="password" name="password" class="form-control" ng-model="formData.password">
            </div>
            <button type="submit" class="btn btn-md btn-success">Connexion</button>
        </form>
        <a href="" ng-click="alert('lalalala')">click</a>
        <img ng-src="images/accueil.png" class="accueil img-responsive">
    </div>
</div>

and here the controller :

login.js

'use strict';

appStat.controller('LoginCtrl', function ($scope, $rootScope, $http, $location) {
    console.log("salut!!");
    $scope.formData   = {};
    $scope.test = "test";
    $scope.toto = function() { alert('alert'); };

   /**
    * Connect l'user
    */
    $scope.formConnexion = function () {...}

  });

and here my app.js : app.js

'use strict';

var appStat = angular.module('posStatsPhoneGap', [
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'ngRoute',
    'ngResource',
    'ui.bootstrap',
    'ngMessages',
    'ngAnimate',
    'ngAria',
    'ngTouch',
    'picardy.fontawesome'
])
    .config(['$routeProvider', '$compileProvider', function ($routeProvider, $compileProvider) {

        $compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|file|tel):/);
        $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|file|tel):/);

        $routeProvider
            .when('/', {
                templateUrl: 'views/login.html',
                controller: 'LoginCtrl'
              })
            .when('/main', {
                templateUrl: 'views/main.html',
                controller: 'MainCtrl'
              })
            .when('/stat', {
                templateUrl: 'views/stat.html',
                controller: 'StatCtrl'
              })
              .otherwise({
                redirectTo: '/'
              });

  }
]);

Thank you in advance !

Following from my comment Try something like this:

  1. Create a Controller file & a controller within it Create a angular

  2. module within the controller file

Create a controller.js file then create a angular module see code below:

 //Within your controller.js file 
  var myApp = angular.module('myApp', []);
   myApp.controller('myController', function($scope, $http){

  $scope.Data = "Alert Alert";

// Create a function within the scope of the module
 $scope.myFunc = function(){

      alert($scope.Data)

  };
});

Then within your HTML file call the function on-Click see code below:

Note: Don't forget to add the controller you created into your HTML code. I usually add it in the body tag

<body ng-controller="myController">
   <button ng-click="myFunc()">Make Request Button</button>
</body>

See a live example here:

http://plnkr.co/edit/sQ0z7TlyWv5fM5XyfujK?p=preview

Also a note: ng-click is mostly used for any click events you are trying to perform within your angular app but ng-submit is mostly used when working with a HTML form submission.

Use ng-submit in you form as:

<form ng-submit="submit()">

 function Ctrl($scope) { $scope.list = []; $scope.text = 'hello'; $scope.submit = function () { if ($scope.text) { $scope.list.push($scope.text); $scope.text = ''; } }; } 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app> <div ng-controller="Ctrl"> <form ng-submit="submit()">Enter text and hit enter: <input type="text" ng-model="text" name="text" /> <input type="submit" id="submit" value="Submit" /> <pre>list={{list}}</pre> </form> <button ng-click="submit()">Submit 2</button> </div> </div> 

I found the error. It was caused by having 2 controllers with the name 'LoginCtrl' . It was a stupid copy/paste error.

I have created one working code for the above question for 4 different type of HTML element, for complete code please follow the below URL -

http://plnkr.co/edit/Tm2Rtbt3xsv4pKzcPQCw?p=preview

<body ng-controller="ExampleController">
    <button ng-click="setButton()">set button</button>
    <div content="snippetContentFirst"></div>
    <div content="snippetContentSecond"></div>
    <div content="snippetContentThird"></div>
    <div>
        <ul content="snippetContentFour"></ul>
    </div>
</body>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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