How do you get a field value from the client script of a widget?

Jonathan18
Giga Contributor

In my client script I am trying to get the value of a dropdown field on my form. This code below works to display the names of the fields.

for (var x in $scope.data.f._fields) {
  alert(x);
}

Here is the html:

<sp-model form-model="data.f" mandatory="mandatory"></sp-model>

And then the server side code produces the form with this line:

data.f = $sp.getForm(data.table, id, '', 'SPView');

I've tried so far with code like:

g_form.getValue([field name here]);

but I'm getting a javascript error that g_form is undefined. I'm wondering if it is due to the construction of the html, and maybe if the form section needs to be wrapped in something or do I need to include a library of some sort?

 

1 ACCEPTED SOLUTION

asifnoor
Kilo Patron

Assuming that your code is in a widget of Service portal, g_form does not work there. to get a value in a client controller you can use something liek this

$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'your_field')
    c.data.yourfield = parms.newValue;
});

Mark the comment as a correct answer and helpful if this helps.

View solution in original post

4 REPLIES 4

Elijah Aromola
Mega Sage

The g_form api is not available in Service Portal.

asifnoor
Kilo Patron

Assuming that your code is in a widget of Service portal, g_form does not work there. to get a value in a client controller you can use something liek this

$scope.$on("field.change", function(evt, parms) {
if (parms.field.name == 'your_field')
    c.data.yourfield = parms.newValue;
});

Mark the comment as a correct answer and helpful if this helps.

Hello Asifnoor, 

how this can be retrieved in html code?
like, 

{{c.data.yourfield}} ?

 

Hi @asifnoor ,

This code is working when we changed the value of the field and save the record but not when we save record without changing the value