i am trying to pass a constant value to a label and trying to access it somewhere else.This is what i have tried
<label ng-model="alertID"> 1 </label>
and i am trying to access it in a controller by passing alertID to a function in controller.but i am getting "undefined" as alertID's value.Following is the function in controller
$scope.saveEventInfo = function(alertID){
var id = alertID;
}
How do i get its value?I dont want to use ng-init
ng-model on a label doesn't make sense because it's not an input and therefore doesn't need two-way binding.
I'm guessing you just want to initialise the value like this:
<label ng-init="alertID = 1">{{ alertID }}</label>
$scope.alertID will now be 1 on your controller.
Related
I have a reactive form instantiated using FormBuilder. I would like to use this form for updating or adding a new employee. I am trying to add an expression to the formBuilder constructor that checks to see whether there is a currently selected employee and if there is use one of that employee's interface fields as the value on the form and if there's no current selected employee have a empty string as the value of the specific FormControl.
what i've tried:
'EMP_NM': [this.selectedEmp ? this.selectedEmp.EMP_NM : '', Validators.required],
and then in my editEmployee function:
editCashier(employee: IEmployee) {
this.selectedEmp = employee;
this.empForm.reset();
console.log(this.selectedEmp.EMP_NM);
}
When I do this the value shown on the EMP_NM input field in the HTML doesn't get updated. i've tried setting a setTimeout() in my editEmployee function but that didn't work either. Any idea what might be going wrong here?
I initialize the empForm in my conponent's constructor using formBuilder's group function. Would it help if I moved that to a different function and then call that function everytime the editEmployee function is called to re-initialize the form?
You can update it with patchValue or setValue:
this.empForm['controls']['employee'].patchValue(employee);
Have no clue if it's a nested control but you can go more layers down.
Would like to know how to set 2d array in ngmodel for dynamic checkbox?
I have a role and permission setup form for a super admin with multiple checkbox.
How to differentiate model name with role id and permission id. I need to pass role id and permission id with the model name in an array.
eg : [(ngModel)]="permission[role.id][per.id]"
Is there a way to assign 2d value for ngmodel in Form?
Looking forward for an earliest response.
Thanks in advance.
As ngModel is directive to obtain 2-way binding system.
You can easily do the syntax:
[(ngModel)]="permission[role.id][per.id]"
where permission is an empty array defined initially as:
permission = []
or
permission = [[]]
where your permission variable will be a multidimensional array.
After this, if you try to ngModel
[(ngModel)]="permission[role.id][per.id]"
it will cause a undefined issue as ngModel not only gets the value from input it has to show also that is how are 2-way binding works.
To avoid issue while rendering due to undefined, we have to assign dummy data to the permission Array initially only.
this.permission = new Array(this.numberOfRoles).fill(0);
for (let j = 0; j < this.numberOfRoles; j++) {
this.permission[j] = new Array(this.numberOfPermission).fill(0);
}
This solves the undefined issue such as below
enter image description here
I'm currently trying to get some values from checkboxes that are "posted" to another aspx page. My code looks something like this:
<form method = "post" action = "HubPageUpdate.aspx">
<input type = 'checkbox' name = 'customerBox' value = '0'>
<input type = 'checkbox' name = 'customerBox' value = '1'>
<input type = 'checkbox' name = 'customerBox' value = '2'>
<input type = 'checkbox' name = 'customerBox' value = '3'>
<input type = 'checkbox' name = 'customerBox' value = '4'>
<input type = "submit" value = "Update">
</form>
My code generates the checkboxes dynamically, so it's not really hard coded. The ASP.NET page it is posting to is then to take what's checked, and I'll does some SQL to it. However, when I was testing it out by just using a simple:
<% Response.Write(Request.Form("customerBox")) %>
It would return to me what's checked as:
1,2,3
Which means the checkbox works, and that delights me. However, because it's a string, I can't really do much with it because I need them individually for SQL queries. How can I get them individually, and not as a string? I first assumed an array, but I still am not sure how to get them individually. Thanks!
You assuming using an array was right (or atleast it's one way to do it). ;)
Not the shortest, but you can try this:
Dim RecievedString As String = "1,2,3"
Dim BoxesChecked() As Integer = New Integer() {}
For Each s As String In RecievedString.Split(",")
Array.Resize(BoxesChecked, BoxesChecked.Length + 1)
BoxesChecked(BoxesChecked.Length - 1) = Integer.Parse(s)
Next
Just assign RecievedString with what you get from the response.
BoxesChecked(0) will give you the first Integer in the array, BoxesChecked(1) the second one and so on...
So you just need a comma-separated string to be integers? Here's how you do that.
String stringValue = "1,2,3";
Dim asInts = From str in stringValue.Split(',')
Select int.Parse(str);
My VB is rusty, so this might not be totally accurate.
How to get 'No User found' message from the below variable in Node.js
{"error":["No User found."]}
You can do it like this:
var data = {"error":["No User found."]};
alert(data.error[0]);
The outer object has a property "error". That property, then contains an array from which you want the first element in the array (index 0).
So, you get the error property with data.error. And, then you reach into the array and get the first element with data.error[0].
You need to iterate through this object with a for loop and grab the value based on the name of the property.
for(item in obj){
console.log(obj[item]);
}
You can use an if statement and the match method if you need to make sure that the property name is 'error'.
Here is my fiddle:
http://jsfiddle.net/nhmaggiej/eajqzxfr/
I'm using Jinja2 and Google App Engine.
I identify my models with integer num:
class TestModel(db.Model):
value = db.StringProperty()
num = db.IntegerProperty()
and have a class for editing the value:
class Edit(Handler):
def get(self):
#show input box with value, let user edit value, render page with num
def post(self):
#get new value, filter database for model instance BY NUM and replace with new value
I need to store num on the page so I can retrieve it for the post method but I don't want the page to display it.
I tried:
<span name = "num" value = "{{num}}"></span>
and in the post method I have
num = self.request.get('num')
logging.error("NUM: %s" % num)
But when I run this, num doesn't show in the logs (I have "NUM: ") and I get
ValueError: invalid literal for int() with base 10: ''
When I render the page, the source code shows that num is there. Why isn't this code properly retrieving num?
Have you tried putting the field into a hidden input element. i.e.
<input type="hidden" name="..." value="..."/>
Also make sure that the value you are trying to retrieve in your post is within the form tags.