3

I have a project where I'm currently trying to refactor an old system that was hinged on jquery from the ground up with angular 1.x. However, there are a lot of old HTML forms that I'd like to reuse the bulk of so I don't want to recreate them. I'd love it if there was a way to keep it purely angular, but I'm honestly at a loss of how I'd do that (or whether or not I can). I'm fairly new to angular so there are a lot of inner workings to it that I'm still not privy to.

I've searched around on google and other places including here and I can't really even find other people talking about it. That tells me that either I'm searching badly or it's something that I should probably not be working towards.

All the html pages have identically id'd fields so I feel I can reliably base things on that. For example: all forms with first name text boxes have an id of "cl_fname".

Is there anyway that I can accomplish: getting the form, adding an ng-model="cl_fname" or something to the relevant tag and then display the form? I've gotten to the point where I can get the html page, hold it in the scope and then display using ng-bind-html, but figuring out how to add angular attributes to specific elements I can't figure out.

1
  • 1
    It would help if you posted a small example of code Commented Oct 26, 2015 at 16:53

1 Answer 1

2

You can achieve this with jQuery and the attr() method.

I created a plunker here that demonstrates adding angular to an existing "plain" html form. In the example, I'm using id selectors, but you could use any combination of selectors to ensure you get the right elements.

The below is a quick code snippet from my Plunker example:

HTML:

<div ng-app="myApp">
    <form id='myForm1' data-test="test2">
      <span>First Name:</span>
      <input type="text" id="myForm1_firstName"  />
      <input type="submit" id="myForm1_Submit" value="Go!" />
    </form>
  </div>

JS:

// set up angular
var myApp = angular.module('myApp', []);
myApp.controller('MyForm1Controller', ['$scope', function($scope) {
  $scope.firstName = 'Angular Working!';
}]);

// use jQuery to add the relevent attributes to our form
var jqMyForm1 = $('form#myForm1');
var jqTxtFirstName = jqMyForm1.find('input[type="text"]#myForm1_firstName');
//add controller to form
jqMyForm1.attr('ng-controller', 'MyForm1Controller');
//bind the textbox to the angular 'firstName' variable
jqTxtFirstName.attr('ng-model', "firstName");

EDIT:
just realised you want to load the html form dynamically. Version 2 of the plunker (here) will now dynamically load a HTML form from an external resource (separate html page), inject it into the current page, add the angular bindings to it, and then get angular to recognise it.

The key to getting angular to recognise the form is the use of the $compile object (angular $compile documentation).

Again, quick snippets of the code in use:

HTML (main page):

  <div ng-app="myApp" ng-controller="LoadingController"></div>

HTML (myForm1.html):

<form id='myForm1' data-test="test2">
  <span>First Name:</span>
  <input type="text" id="myForm1_firstName"  />
  <input type="submit" id="myForm1_Submit" value="Go!" />
</form>

JS:

// set up angular
var myApp = angular.module('myApp', []);
// main controller for loading the dynamic form
myApp.controller('LoadingController', ['$scope','$http','$compile', function($scope,$http,$compile) {
  $scope.loadHtmlForm = function(formURL) {
    $http.get(formURL).then(function successCallback(response){
      var jqForm = $(response.data);

      var jqTxtFirstName = jqForm.find('input[type="text"]#myForm1_firstName');
      //add controller to form
      jqForm.attr('ng-controller', 'MyForm1Controller');
      //bind the textbox to the angular 'firstName' variable
      jqTxtFirstName.attr('ng-model', "firstName");

      $('div').append(jqForm);
      $compile(jqForm[0])($scope);
    });
  }

  $scope.loadHtmlForm('myForm1.html');
}]);

// form controller for managing the data
myApp.controller('MyForm1Controller', ['$scope', function($scope) {
  $scope.firstName = 'Angular Working!';

}]);
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be along the lines of exactly what I was looking for. After I posted the original question, I had obviously kept trying my hand at getting it to work, and funny enough it was looking roughly like what you've given. Thanks a bunch, I know it didn't give too much to go on. I'm marking yours as the answer unless someone comes along with something, somehow not using jquery.

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.