3

I have a page where I'm reverse engineering in Angular JS. All the content is being echoed in by a PHP script (bad practice I know, but I'm easing into Angular and just wanted to use it for the onBlur event).

I was trying to figure out how to pass a variable from PHP to Angular. The variable (customer email) wasn't editable so I wanted to put it in a hidden field. The issue is that ng-model doesn't bind to hidden inputs. I needed another solution..

3
  • 1
    Just echo or print your PHP inside your onblur Event. Even if your page is a separate .js file, you can change it to .php. Just make sure in you do: <script type='text/javascript' src='whatever.php'></script> . Commented Mar 27, 2014 at 1:44
  • That would work if the variable was the same for all rows. In this case I have a table with customer data. I want to be able to edit the customer credit field, so I set up a text input in that column. The customer id and the customer credit values both need to be passed to the angular JS function. Each row will have different functions so it's not possible to hardcode the PHP into the .js file. Commented Mar 27, 2014 at 5:22
  • It's still possible. Get creative. Maybe use a loop. Commented Mar 28, 2014 at 0:15

2 Answers 2

7

I found a great solution here

I ended up not creating a separate init function in my controller but simply did it inline in the ng-init param of a hidden input:

<td><?php echo $db_unit[$i]['email_id']; ?><input type="hidden" ng-init="customer.email='<?php echo $db_unit[$i]['email_id']; ?>'"></td>

One obvious thing I tripped over was that you have to put the value for customer.email in '' I had it without quotes before that and it didn't work.

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

Comments

3

in this example I am passing the PHP $something variable to angular

ng-init="something='<?php echo $something ?>'"

now from the controller do:

console.log($scope.something);

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.