bourax bourax - 4 months ago 11
Javascript Question

compare PHP and AngularJS values

I use jQuery-upload script to manage some users photos, I have a simple html script where I use some AngularJS data:

<input type="radio" name='profile_pic' value="{{file.name}}">


I want to check if the
{{file.name}} == $userProfile['profile_pic']
,
the direct comparison doesn't work, how can I use
ng-model
or
ng-init
or
ng-if
to add "checked" to the radio option when the value of the radio is the same as $userProfile['profile_pic'] ?
I tried this but it doesn't work, I know PHP reads {{file.name}} as a string itself then AngularJS replaces with its value:

<?php
$checked = '';
if ('{{file.name}}'== $userProfile['profile_pic']){
$checked ='checked'}
?>
<input type="radio" name='profile_pic' value="{{file.name}}" <?= $checked ?>>

Jai Jai
Answer

It's not the way to compare two different values. PHP runs at server while angular runs in browser way later.
So simply you can't compare these two different scoped values like this. But there you have $http service to send a request to server, compare it there and send a response at the client.

What can be done:

  1. Create a service/factory which sends a request to the server with the data you want to send.
  2. Do compare at the server and create a response for the client.
  3. In the success handler you can update the model for any change reflection.