Passing SESSION variable into angularjs textbox value - angularjs

Is it possible to pass the userID from the session to the AngularJS textbox value? I have tried to put the echo call into the value="" but it doesn't work.
<input type="text" data-ng-model="user" id="user"
value="<?php echo $user; ?>" disabled/>
<button data-ng-click="add(userID)">Add</button>

You can not use php variable in angular template. As this template is render by angular engine so it will interpolate only scope variables. Use services for your data to get it from php and use it through your controller.
Ref - Take a look

Related

checkbox remain checked after submit laravel

I have this simple checkbox.
My problem is, if I select any of the checkbox, it should remain checked after form submission.
<label>
<input type="checkbox" name="services_offered[]" value="water jet cutting"> Water Jet Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="plasma cutting"> Plasma Cutting</label>
<label>
<input type="checkbox" name="services_offered[]" value="CNC router cutting"> CNC Router Cutting</label>
I tried to use my code before but no luck,
<?php
function isChecked($value)
{
return (!empty($_REQUEST['services_offered']) && in_array($value,$_REQUEST['services_offered']));
}
<label><input type="checkbox" name="services_offered[]" value="water jet cutting" <?php if(isChecked('water jet cutting')) echo ' checked' ?>> Water Jet Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="plasma cutting" <?php if(isChecked('plasma cutting')) echo ' checked' ?> > Plasma Cutting</label>
<label><input type="checkbox" name="services_offered[]" value="CNC router cutting" <?php if(isChecked('CNC router cutting')) echo ' checked' ?>> CNC Router Cutting</label>
I want it to convert in "laravel way" but I dont know how.
Hope you understand me.
Thanks.
I don't know if I understand you correct but:
The form does not post values of the checkboxes.
So I made hidden inputs associated with each checkbox and maintain there values with javascript something like:
$("#services_offered").change(function() {
if(this.checked) {
$('#services_offeredHidden').attr('value','1');
}
else {
$('#services_offeredHidden').attr('value','0');
}
});
In laravel controller I play only with hidden inputs which I get the from Request $request.
When you return the view from a controller you do like this
return view('nameOfView')->with('checkbox',$checkboxValue);
and the in the blade you can access the value using:
{{$checkbox}}
Hope it helps!
If you want the "Laravel way" of doing things then I suggest you check out the HTML package by Laravel Collective for marking up the HTML forms in your view. Also make sure the methods on your controller are receiving a Request as it's argument and maybe even checkout Laravel's built-in Form Request Validation as well.
Note, your current code is very old fashioned so it might take a while to learn how to do things the modern way but stick with it and you will be rewarded.
In your view you will open your form like this:
{!! Form::open(['url' => 'foo/bar']) !!}
//
{!! Form::close() !!}
Your method on the controller that receives the form will look something like this:
public function store(Request $request)
{
$name = $request->input('name');
//
}
But in general, because your question is too broad to answer clearly without basically re-writing your code for you, you need to start doing some tutorials to learn Laravel properly. I recommend Jeff Way's video tutorial courses on Laracasts.
I had a similar problem - here is how I solved it:
My blade template rendered a list of checkboxes as html:
<label for="sId1"><input type='checkbox' name='sIds[]' id="sId1" value="1" checked >Draft</label>
<label for="sId2"><input type='checkbox' name='sIds[]' id="sId2" value="2" checked >Submitted</label>
I wanted to a) default them all to checked; and b) preserve their checked/unchecked status between validations.
The key is to use the laravel helper function old() - and to realise the old() function returns an array - so you need to do an in_array(value,old()). You also need to cover when the old() is empty on first load by checking is old()==null:
Here is the blade template snippet I used to do it:
#foreach($wfStatusIdsArray as $wfsId=>$statusName)
<label for="sId{{$wfsId}}"><input type='checkbox' #if(old('sIds')==NULL || in_array($wfsId,old('sIds'))) checked #endif name='sIds[]' id="sId{{$wfsId}}" value="{{$wfsId}}">{{$statusName}}</label>
#endforeach
There is a bug in this - if ALL checkboxes are cleared, on validation, the checkboxes are reloaded as all ticked. This is fine for my situation so I have not fixed it. Let me know if you fix that bug!

How to get values of input in Angular JS?

In Angular JS are created inputs.
<input type="text">
<input type="text">
How I can to get values from each inputs and send to server?
I tried:
<input type="text" ng-model="typeInput">
But I get value only one field.
In server I am planning to get data format: $_POST['type'][0] = 'One value'; $_POST['type'][1] = 'Two value';
I mean, that I need send to server array of values. For example:
name="type[]" value="1"
name="type[]" value="2"
name="type[]" value="3"
So, in server I cando loop for check:
foreach($_POST['type'] as $val){
//prepare
}
Using the ng-model binds it to the property of the same name in the $scope of the controller.
You would access it in your controller like so:
$scope.typeInput
Or you can access it in your HTML like so:
{{typeInput}}
To access both fields, you would need to give them both a different property name to bind to.
<input type="text" ng-model="firstName">
<input type="text" ng-model="lastName">
...
alert($scope.firstName);
alert($scope.lastName);
More complete info here: https://docs.angularjs.org/api/ng/directive/ngModel
Edit: Here's an example fiddle with binding your inputs to an array: https://jsfiddle.net/gxmw62rj/2/

Assigning AngularJS model inside ngRepeat

I would like to iterate over an array in AngularJS like this:
var languages = ['en', 'es', 'fr'];
To generate something like this:
<input type="text" ng-model="mymodel.en" placeholder="Name (EN)">
<input type="text" ng-model="mymodel.es" placeholder="Name (ES)">
<input type="text" ng-model="mymodel.fr" placeholder="Name (FR)">
I have tried this:
<div ng-repeat="language in languages">
<input type="text" ng-model="mymodel.{{language}}" placeholder="Name ({{language | uppercase}})">
</div>
But throws an error:
"Syntax Error: Token '{' is an unexpected token at column ..."
How should I perform this loop?
Use mymodel[language] instead of mymodel.{{language}}
<div ng-repeat="language in languages">
<input type="text" ng-model="mymodel[language]" placeholder="Name ({{language | uppercase}})">
</div>
plnkr
See the plnkr, and start typing in any of the input fields to see the model changes.
you cantry something like this,
<input type="text" ng-model="mymodel[language]" ...
You shouldn't use {{}} notation inside ng-model here you can refer to your property in the mymodel object.
But it seems that you are trying to get binding via model somehow for your inputs. It's not gonna work in your case because you need to init mg-model directives.
For catching changed in your model from dynamically generated you need to compile used directives using $compile service.
Here you can check out sample. I googled it but I've solved similar problem in similar way.

AngularJS file upload is not working when we use ng-change

In file upload ng-change is not working as expected in angularJS
<input type="file" ng-model="fileUpload" ng-change="setFiles(this) /"
JS:
$scope.setFiles=function(element){
console.log(element.files);
}
Here element.files is undefined
But if i chage the ng-change to onchange it's working.
<input type="file" ng-model="fileUpload" onchange="angular.element(this).scope().setFiles(this)"/>
JS:
$scope.setFiles=function(element){
console.log(element.files);
}
Here i'm getting the element.files object.
Why it is working in onchange not in ng-change?
Angular doesn't seem to support binding for <input type="file"..../>. It seems like you have to create a directive... full details here. You can also try this library
<input type="file" id="file_upload_id_here" style="width:55%" name="file" onchange="angular.element(document.getElementById('file_upload_id_here')).scope().getFileDetailsCandidate(document.getElementById('file_upload_id_here'))" />
use like the above, some times the root scope is switchesm it cannot revert it current scope, on that this functionality will not works, so put your current document object like the above

Set a default value with angular js

I have an field where the id is set via php:
<input name="user_id" type="text" value="<?php echo $this->r->user_id; ?>" ng-model="entry.user_id" >
But the value does not appear in the field.
How can I set it from php to angular js?
Set the initial value of the ng-model using ng-init instead:
<input name="user_id" type="text" ng-model="entry.user_id" ng-init="entry.user_id = <?php echo $this->r->user_id; ?>" >
I'm not sure about the php syntax, but this will set value for you as long as the controller has an entry object available.
<input type="text" ng-model="enquiry.companyName" ng-init="enquiry.companyName='<?php echo $_SESSION['COMPANYNAME'] ?>'" name="companyName">
Sigle quote around php block is important.
You might want to try ng-value as well. Might fit your situation better. I didn't come across it until today:
<input name="user_id" type="text" ng-value="<?php echo $this->r->user_id; ?>" ng-model="entry.user_id" >

Resources