0

Currently i'm trying to send a time and a date from php to my angular controller by using a ng-click.

$time is a date
$today is a datetime

Can someone explain me why this works

<button ng-click="getClickInfo('<?php echo $time; ?>', '<?php echo $today; ?>')";</button>

But when i try to achieve the same result within an echo like this, it gives me different and incorrect output

<?php echo '<button ng-click="getClickInfo(' . $time . ', ' . $today . ')";></button>'; ?>

I tried to search on the internet for a solution but i couldn't really find a topic about it. Hope someone can explain me what is happening in this scenario. Redirection to any articles on this topic would be really helpfull for me.

Output:

01:00 // incorrect output
01/01/1970 // incorrect output

20.30 // desired output
22-04-2016 // desired output
5
  • Your html string is not correct. It should be: '<button ng-click="getClickInfo(' . $time . ', ' . $today . ')"></button>' I can't say for sure that is your exact issue, but for correct HTML, that needs to be fixed Commented Apr 21, 2016 at 17:28
  • Fixed the html string but still returns me the same incorrect result Commented Apr 22, 2016 at 7:35
  • I see that the quotes are missing if i inspect my code, but i still have no idea if there is a fix for it. Example one (correct example) is like ('20.30', '22-04-2016'), example two is like (20.30, 22-04-2016). This probably is causing the problem. Commented Apr 22, 2016 at 7:47
  • 1
    why to use php and angular together ? angular views should get there data from $scope. this $scope should be initialised in controllers side (with http query for example ). Commented Apr 22, 2016 at 8:18
  • Good question, i already had made a php class that renders certains dates and times for a calender. I've used more php than angular yet, but now i read your comment it seems obvious for me to remake those php functions as angular functions for best practice. I simply miss some angular expierence yet i'm trying to obtain. Commented Apr 22, 2016 at 8:37

1 Answer 1

1

You have to quote the javascript function arguments, otherwise you get unpredictable results The ctach here is that the HTML ng-click attribute must be enclosed in double quotes and the attribute value (your function) must not contain double quotes, because it would break the HTML

Furthermore, the ; is not needed, you're putting it outside the ng-click HTML attribute value, that is not valid HTML

Here's a fix, note that quotes are escaped with backslash inside PHP strings:

<?php echo '<button ng-click="getClickInfo(\'' . $time . '\', \'' . $today . '\')"></button>' ?>

Here's a more readable way to do it, I would recommend this approach:

<?php echo '<button ng-click="getClickInfo(' . "'$time', '$today'" . ')";></button>' ?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot, i was looking for something like this but i simply didn't know how to do it. This pretty much solved it.

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.