i've been going from page to page all over the internet trying to find a simple and recommended way of uploading a single file in angularjs, so far i have found none, what i want is to be able to do something like this:
<input type="file" ng-model="file" id="form.file" />
<input type="text" ng-model="name" id="form.name" />
Then in my controller/service i have a function that posts all my form data:
SomeRandomService.save = function($scope.form) {
return $http
.post('/api/v1/some/random/url/', $scope.form)
};
Is this so difficult in angular? I am a newbie to angular so i can't even understand some of the solutions i have found online.
Is there something way more simpler? A plugin or service or directive that can do this for me?
Related
I currently have a html web application which contains a hidden form:
<form id="loginForm" action="" method="post">
<input id="login" name="login" type="hidden" placeholder="Login">
<input id="pwd" name="pwd" type="hidden" placeholder="Password">
</form>
I use Javascript to populate 2 inputs (login, pwd) programatically. Then i look up the address of the device i want to post the information to (dynamic IP address) and post using javascript:
$('#loginForm').attr('action', data.address + "/login");
$('form').submit();
This works well, the device is an embedded controller, and logs me in.
I'm developing an App using codename one to mimic my web application, can i acheive the same somehow using browser component?
Thanks
No need for a form.
If I understood the code correctly you can use something like this:
String address = Socket.getHostOrIP();
Rest.post("https://myurl")
.queryParam("action", address + "/login")
.fetchAsString(response -> {});
I'm not sure about the address line, it will return the local device IP which seems weird for this use case.
This can't be correct, perhaps my google has gone off the rails but for the life of me I can't find any documentation to return all $valid = true values using the FormController. Does this functionality exist? I'm trying to marshall information from a form and send it to my web service but I wan't to make it something sensible before doing so. Currently I'm looping through all the properties in the FormController for a given form and looking to see if it doesn't start with $, and $valid = true and pushing it into an array like so:
angular.forEach(form,function(data,key)
{
if(key.indexOf('$') === -1 && data.$valid)
{
var item[key] = data.$modelValue;
clean.push(item);
}
})
Real basic, but I'm totally stumped (and can't fully believe) that this doesn't exist already in the angular API somewhere. Am I missing something? I'm still learning a lot about angular and am getting the feeling that much isn't really documented but perhaps i'm missing something quite basic. Thanks for reading!
This is not what ngFormController is used for (for validation and custom directive scripting). If you need to collection form data to send to server all you need is do is to make use of ngModel directives:
<form novalidate ng-submit="saveUser()">
<input type="text" ng-model="user.name">
<input type="text" ng-model="user.email">
</form>
and in controller saveUser handler you can access form data like
$scope.saveUser = function() {
console.log($scope.user); // {name: "Thomas", email: "mann#ga.com"}
// use $scope.user data to send to server
};
You just need to use ng-model. Angular won't populate the model until the value in the form is valid, so you don't need to check $valid manually.
Ex:
<input type="text" ng-model="myData" ng-minlength="3" required>
{{myData}}
You'll see myData isn't populated until you've entered at least 3 characters.
I know case how do this in Jquery by cheking event keypress, but how do it in Angular JS? I want to send to server AJAX request for searching data via ElasticSearch. Need I use timeout in typing text event?
Is enought example from tutorial? This example shows value in template, but I need get in controller
Enter your name: <input type="text" ng-model="name"></input>
<p>Hello {{name}}!</p>
Use the ngChange directive:
https://docs.angularjs.org/api/ng/directive/ngChange
<input type="text" ng-change="nameChanged()" ng-model="name"></input>
I have already solved this problem, but it took me a while to realize what I did wrong. It's a very simple mistake, but I figured I'd post it here in hopes I can save someone else some work in case they run across the same mistake.
I was trying to use simple Angular validation to set a class on an input field based on whether it was valid. I failed to realize it wasn't working because I specified the name of my form with ng-form. So using $scope.form or the actual value of name attribute of the form did not work. Of course, the examples below are simplified and a much larger form could make this mistake much harder to recognize.
Here is a failed example:
<form name="myForm" ng-form="form1">
<input type="text" name="myField" ng-class="error: myForm.myField.$invalid"/>
</form>
Here is a successful example:
<form name="myForm" ng-form="form1">
<input type="text" name="myField" ng-class="error: form1.myField.$invalid"/>
</form>
I was working on a simple angular functionality. but got stuck.
e.g.
<input type="radio" data-ng-model="package" value="1" data-more="some text here" />
Is there a way to retrieve the more data?
I know I can get the value by this using this: {{package}}
Cheers