2

Currently, I am using a server side framework CakePHP to build my app.

I need to integrate angular into my app.

Currently, I send the following inputs

User.old_password, User.new_password, User.new_password_confirm

to this url /mypasswords/new using POST

This works just fine.

I understand that to send data to server side I need to use services in Angular.

This is my Angular code so far.

var app = angular.module("myApp", []);

app.controller('passwordController', ['$scope', function($scope) {
  $scope.submitted = false;
  $scope.changePasswordForm = function() {
    if ($scope.change_password_form.$valid) {
      // Submit as normal
    } else {
      $scope.change_password_form.submitted = true;
    }
  }
}]);

services = angular.module('myApp.services', ['ngResource']);

services.factory("UserService", function($http, $q) {
  var service;
  // service code here
});

So what do I write in the //service code here part? What other parts am I missing?

My form currently looks like this as html after being rendered. (I know I probably have to remove action and method attributes from my form. Do I?)

<form action="/mypasswords/new" id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            

<input name="data[User][old_password]" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
<input name="data[User][new_password]" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
<input name="data[User][new_password_confirm]" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
<div id="u25" class="u25_container">
    <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
     </div>
</div>

<div id="u27" class="u27_container">
    <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         

</div>
</form>

Thank you.

2 Answers 2

1

if you want just to send the form with ajax, No need to create factory, just do it in controller like this:

var app = angular.module("myApp", [$http]);

app.controller('passwordController', ['$scope', function($scope) {
  $scope.submitted = false;
  $scope.changePasswordForm = function() {
    if ($scope.change_password_form.$valid) {
      //note: use full url, not partial....
      $http.post('http://myweb.com/mypasswords/new', change_password_form.Data) 
      .success(function(data, status, headers, config) {
        //do anything when it success..
      })
      .error(function(data, status, headers, config){
        //do anything when errors...
      });
    } else {
      $scope.change_password_form.submitted = true;
    }
  }
}]);

And your form will looks like this:

<form id="change_password_form" name="change_password_form" ng-controller="passwordController" ng-submit="changePasswordForm()" method="post" accept-charset="utf-8" class="ng-scope ng-pristine ng-valid">

<div style="display:none;"><input type="hidden" name="_method" value="POST"></div>            

<input name="data[User][old_password]" ng-model="Data.old_password" id="u56" placeholder="Enter old password..." class="u56 profile_input" type="password">
<input name="data[User][new_password]" ng-model="Data.new_password" id="u57" placeholder="Enter new password..." class="u57 profile_input" type="password">
<input name="data[User][new_password_confirm]" ng-model="Data.new_password_confirm" id="u58" placeholder="Enter new password..." class="u58 profile_input" type="password">
<div id="u25" class="u25_container">
    <div id="u25_img" class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
     </div>
</div>

<div id="u27" class="u27_container">
    <input id="u27_img" class="submit_button detectCanvas" type="submit" value="SAVE">                         

</div>
</form>
Sign up to request clarification or add additional context in comments.

6 Comments

Why should I use full path? When to use factory? When not to use factory?
1) because you don't specify the ng-app config, 2) it's just like a class, you need it to separate boiler plate code.
by ng-app config, you mean something like this? gist.github.com/simkimsia/f055fead5a18da33825e
and maybe you found this useful: github.com/mgonto/restangular i always use that to my projects
|
1

Step 1: write form using normal html instead of FormHelper in CakePHP

The form should look like this:

<form name="change_password_form" ng-controller="passwordController"
         ng-submit="changePasswordForm()">

        <input name="data[User][old_password]" ng-model="data.User.old_password" type="password" placeholder="Enter old password..." class="u56 profile_input">
        <input name="data[User][new_password]" ng-model="data.User.new_password" type="password" placeholder="Enter new password..." class="u57 profile_input">
        <input name="data[User][new_password_confirm]" ng-model="data.User.new_password_confirm" type="password" placeholder="Enter new password again..." class="u58 profile_input">

            <div id="u25"
                 class="u25_container">
                <div id="u25_img"
                     class="clear_all_button detectCanvas" onclick="document.getElementById('change_password_form').reset()">
                </div>
            </div>

            <div id="u27"
                 class="u27_container">
                <button type="submit" id="u27_img" ng-click="debug()"
                     class="submit_button detectCanvas" />
            </div>
        </form>

Step 2: write the app.js in the following manner.

the X-Requested-With header is important if you want to use the CakePHP $this->request->is('ajax')

angular.module("myApp", [])
.controller('passwordController', function($scope, $http) {
  $scope.changePasswordForm = function() {
    console.log('change passwrd form activated');
    //note: use full url, not partial....
    $http({
        method  : 'POST',
        url     : '/mypasswords/new',
        data    : $.param($scope.data),  // pass in data as strings
        headers : { 'Content-Type': 'application/x-www-form-urlencoded',
        'X-Requested-With' : 'XMLHttpRequest'
        }  // set the headers so angular passing info as form data (not request payload)
    })
    .success(function(data, status, headers, config) {
        //do anything when it success..
        console.log('works!');
      })
    .error(function(data, status, headers, config){
        //do anything when errors...
        console.log('errors!');
    });
  }
  $scope.debug = function () {
   console.log($scope);
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.