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..

jeznag
  • 4,183
  • 7
  • 35
  • 53
  • 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: `` . – StackSlave Mar 27 '14 at 01: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. – jeznag Mar 27 '14 at 05:22
  • It's still possible. Get creative. Maybe use a loop. – StackSlave Mar 28 '14 at 00:15

2 Answers2

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.

jeznag
  • 4,183
  • 7
  • 35
  • 53
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);
Mahmoud Zalt
  • 30,478
  • 7
  • 87
  • 83