1

I have a simple scenario in a large piece of code:

  1. Angular APP & Controller (all working well)
  2. one ng-model element is dynamically created later (or lets say it comes from an ajax call). How to bind this into that controller?

For example:

var dtApp = angular.module('App1', []);
dtApp.controller('Cont1', function($scope)
{
  .
  .
  .
  .
  $scope.xyz='1'; 
}
.
.
.
//element created dynamically on click: 
<input type="text" ng-model="xyz" />
.

This is just an example, but it will solve the actual and larger problem if I know the solution.

Thanks a lot.

4 Answers 4

0

You need to use $compile. See AngularJS - Dynamically creating elements that specify directives

Sign up to request clarification or add additional context in comments.

Comments

0

if you want to manually add DOM element to DOM tree of the controller after it finishes binding & compiling phase, you will need to compile and bind the scope of the controller to the new DOM element yourself using $compile service : https://docs.angularjs.org/api/ng/service/$compile

var dtApp = angular.module('App1', []);
dtApp.controller('Cont1', function($scope, $compile)
{
   var parent_element_to_append = angular.element(YOUR_SELECTOR);
   var element = $compile("<input type="text" ng-model="xyz" />")($scope);
   parent_element_to_append.append(element);
}

1 Comment

I like the idea, but there is a huge code (working well) that creates the element(s) and its not inside angular. So thats the problem
0

Why don't you just play with element visibility instead of generating it ng-show/ng-hide/ng-if. If the request is outside the controller's visibility you can trigger an event and listen to it and then make the element visible. This way the you will have the element from the beginning it just won't be visible but ng-model will bind your property. This won't work only if you don't know what element will be generated (if there are a lot of possibilities), but if there are a small number you can add all of them and only make the one you want visible.

2 Comments

The element(s) are created dynamically. its not 1 element
Ok. So your request have the element sent as string? If that is so, instead of generating it outside angular scope, why don't you trigger an angular event which executes the piece of code that Thai suggested you in the answers above? Sorry for that i'm not writing in the Thai's answer, but I don't have reputation to write comments on other users answers.
0

With some help from other posts (and some hints from the other answers), here is the simplest solution:

1. at JS code that triggers new dom element

angular.element(document.getElementById("Cont1")).scope().bindx('new_field_id');

2. inside Controller Cont1

//first add $compile in the function definition, then the following
$scope.bindx = function(ele_id)
{
    ele = $('#'+ele_id).parent();
    $compile(angular.element(ele).contents())($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.