Find form $error length - angularjs

I'm attempting to get the number of errors in $scope.someForm.$error and can't figure out a way to get the count.
Example $error:
$scope.someForm.$error
Object{date-disabled: Array[1], required: Array[1]}
Expected Result is 2.
Is there a way to get the count of objects that resulted in an error?
I'm trying to do this because I ran into an issue with bootstraps date-picker invalidating my angular form.
https://github.com/angular-ui/bootstrap/issues/3439
Now I want to code around the issue with a check. I want to say $scope.someForm.$error count is 1 and is date-disabled continue with form submission.

Iterate the object keys and total the array lengths:
var totalErrors = 0;
for (var error in $scope.someForm.$error) {
if ($scope.someForm.$error.hasOwnProperty(error)) {
totalErrors += $scope.someForm.$error[error].length;
}
}

Related

Google Script is returning the index of the array but I need the value

I have a google spreadsheet that gets data logged to it via a google form.
When the form is logged each time, it triggers a script that gets values from a certain section using:
var tabNumsVal = sheet.getSheetValues(lastRow, tabOneCol.getColumn(), 1, 6)[0];
When I check the array, I can see that the array has the values such as:
0: 12
1: 24
2: 26W
3: 0
4: 0
5: 0
However when I use the following command, it puts the index numbers (0 to 5) into the array instead of the values in the array.
var tabNumsFinal = [];
for (var tabard in tabNumsVal) {
if (tabard !== "") {
tabNumsFinal.push(tabard);
}
}
It used to work but I have had to upgrade my code to Google Script v8 and it seems that this has broken the code.
I had to alter the 'for each' code block to a 'for' code block and it seems this is handling the values differently.
I am quite sure this is simple for many people but I really only touch Google Script 1 time each year. I have tried using Logger.log(tabard) to output the data to the execution log, but it just traverses the code and doesn't output anything. I figured this might be because of the !== "" operator, so I placed it above the if statement but still inside the for statement and it still outputs nothing.
I tried using Logger.log(tabNumsVal) and Logger.log(tabNumsFinal) and again it output nothing.
To recap:
The data from the form is returning correctly into the columns of the spreadsheet, hence it is showing inside the array properly. It's just that the index numbers are being output instead of the values from the array.
Since you're using for in loop, tabard is the index here.
var tabNumsFinal = [];
for (var i in tabNumsVal) {
let val = tabNumsVal[i];
if (val !== "") {
tabNumsFinal.push(val);
}
}
For in loop

Freezing rows in Sheets with Google Apps throws type error

I'm trying to use GAS to freeze the top row of each sheet. It works, freezes the desired rows, but returns an error:
"TypeError: cannot call method setFrozenRows" of undefined (line6, file "freezeLabelRows")
According to Google documentation, the syntax is correct.
I'm running the script from the code editor attached to the sheet where I'm developing the app.
I tried a number (1) where numRowsFr is now; that was a workaround I used to dodge this error.
function rowFreeze() {
var numSheets = SpreadsheetApp.getActiveSpreadsheet().getNumSheets();
for(var i = 0; i <= numSheets; i++) {
var frSheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[i];
var numRowsFr = 1;
frSheet.setFrozenRows(numRowsFr);
}
}
As I said, the code works to freeze the desired row on each sheet, but returns an error. I'd like to get the rest of this app in place to upgrade for current users.
Issue:
Array index starts at 0 and ends at length of array -1. You're looping after the end of the array(sheets array) when you use <=numSheets as the loop condition. After the last sheet, frsheet will be undefined and undefined doesn't have a setFrozenRows method as it's not a sheet type.
Solution:
Loop only till the end of the array.
Snippet:
i <= numSheets - 1;
or
i < numSheets;

AngualrJS filter records that being with

I have a field in my AngularJS scope called rid
This is always a 4 letter code such as 1A22 or 4D23 (number, letter, number, number)
I want to filter out (i.e not show) any that meet certain conditions
begin with a 2K
begin with a 5
begin with 1C
Now I know I can build individual filters onto my ng-repeat (service in services), but i think the way forward is a nice custom filter where I can feed in an array of conditions that i want to filter out. I suspect a string of regex conditions is the answer
/^2k|5|1C*/
Can anyone advise the best way to approach this custom filter? Thanks
EDIT : This was my solution, regex from the accepted answer and the following filter
myApp.filter('regex', function() {
return function(input, field, regex) {
var patt = new RegExp(regex);
var out = [];
for (var i = 0; i < input.length; i++){
if(!patt.test(input[i][field]))
out.push(input[i]);
}
return out;
};
});
then I just use this in my ng-repeat
regex:'rid':'^(?:2K|5|1C)[A-Z0-9]+$'"
You were not so far. This regex will match any text starting with 2K, 5 or 1C:
^(?:2K|5|1C)[A-Z0-9]+$
See the demo
If you want to make sure the length is 4, you can use this one:
^(?=[A-Z0-9]{4}$)(?:2K|5|1C)[A-Z0-9]+$
See the demo

Getting the sum of the values of the properties of an object

I'm looking to take the result of a survey that I created using JSON, add up the values associated with the objects from that survey in order to produce a sum. However, I am unsure of how to call upon the items within the Survey Result
I would apprecaite any help.
Survey Result: {
"Number of Polarization Images":1,
"Possibility of Primes":2,
"Available Layers":2,
"Image Quality":3,
"Lead Rank":3,
"Repairs Report":2,
"Topography":2,
"AOI Size":1,
"Field Verification":2,
"Season":1
}
Use .reduce combined with Object.values for browsers that support it
var sum = Object.values( survey_results_variable ).reduce((p,v)=>p+v,0);
For browsers that do not support Object.values use Object.keys
var sum = Object.keys( survey_results ).reduce((p,v)=>p+survey_results[v],0);
Simple solution is
var sum = 0;
for (k in survey_result) sum += survey_result[k];
The bit you might have been missing was the for...in way of iterating over properties in an object.

Binding multiple input box values to a list

I have three input fields - Range1, Range2 and Range3 and an object called pconfig. I have a List named Range in this object. What I am trying to do is to bind these three input fields to this single list but so far I've failed to do so. Is there anyway I can do it using Angular in jade template?
- for (count = 1; count < 4; ++count) {
.form-group
label Range #{count}
input.form-control(type='text' ng-model='pconfig.range' placeholder='Upper Range')
- }
What error are you getting?
I don't know what language/script you are using. But I believe the ng-model name should be with array index for Array List.
type='text' ng-model='pconfig.range[#{count}]' placeholder='Upper Range'
(Not sure how to express count value there. Just a guess)

Resources