AngularJS, how to use capturing groups in Regex - angularjs

In my project, a user can enter a string, which is then translated in the view to a button.
Here is an example, for what I wish to happen:
User types in: argumentation-link_to(80)
In the view, it should look like this:
<button ng-click="goToArgumentation(80)"
class="btn btn-xs btn-info"> a link
</button>
This is my code right now:
$scope.buttonmaker = function(haystack) {
needle = /argumentation-link_to\(\d+\)/i; // <-- Regex
haystack = $sanitize(haystack);
return $sce.trustAsHtml(haystack.replace(new RegExp(needle, "gi"), function(match) {
console.log(match);
return '<button ng-click="goToArgumentation(' + match[0] + ')" class="btn btn-xs btn-info"> a link </button>'
}));
};
At the moment, argumentation-link_to(80) will produce this:
<button ng-click="goToArgumentation(a)"
class="btn btn-xs btn-info"> a link
</button>
I need to get the number 80 in argumentation-link_to(80) to fill in the argument in goToArgumentation()
Thankfully, Regex gives us the option to retrieve data within the Regex with capturing groups. However, when I try to use them, I get the error, that the regex is not valid. Using this site, I tried:
/argumentation-link_to\((?<id>\d+)\)/i
/argumentation-link_to\((?'id'\d+)\)/i
Here I tried one, where no error occured, but I could not get the value:
/argumentation-link_to\((?:\d+)\)/i
What is the correct way to use capturing groups in angular js? And how would I reference them?

To use capture groups you use () and the the exec() function of RegExp:
$scope.buttonmaker = function(haystack) {
needle = /argumentation-link_to\((\d+)\)/i; // <-- Regex
haystack = $sanitize(haystack);
return $sce.trustAsHtml(haystack.replace(new RegExp(needle, "gi"), function(match) {
console.log(match);
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ')" class="btn btn-xs btn-info"> a link </button>'
}));
};

Related

ng-show doesn't show/hide input button

I am implementing an input button, which is visible only if function returns "True" from the back-end.
//Angular Method which returns either true or false, Which is working fine
//Show/Hide Printer
$scope.showHideFunc = function () {
if ($scope.Foo.id != null && $scope.Foo.id != '') {
$http.get(getTFfromDBURL + '/' + $scope.Foo.id).success(function
(data) {
$scope.showHide = data;
});
}
};
//Here is the code for button
<button id="btn-add-device" class="btn btn-info" ng-show="showHide" ng-click="showManagePrinter();loadDrawersForPrinter()">
<span class="glyphicon glyphicon-plus"></span> {{showHide}}
</button>
After all its weird at {{showHide}}, where its getting correct values, while its not affecting ng-show="showHide".
Appreciate thoughts.
Thank you.

Protractor - Drop down Element Not Visible(auto complete drop down)

I have been facing different diff kind of element not visible issues in my application. kindly help me for solution on this.
below or the html code
For Drop Down
<span class="btn btn-default form-control ui-select-toggle" style="outline: 0;" ng-click="$select.activate()" ng-disabled="$select.disabled" aria-label="Select box activate" tabindex="-1">
To Type text in the Auto Complete Drop down.
<span class="ui-select-placeholder text-muted ng-binding" ng-show="$select.isEmpty()">Select Reseller...</span>
I was able to click on the drop down and enter the text. but after that it fails with element not visible error
If u can click on the Auto complete drop down then try to select the drop down by text value.
by.linkText('Text to be selected'));
or
try using the below function (this will select the value by text)
this.SelectRowByCellValue = function (Elem, Texts)
{
Elem.filter(function (element) {
return element.getText().then(function (text) {
if (text == Texts && text != null)
{
element.click();
return false;
}
else
{
}
});
}).then(function (filteredElements) {
});
};

cannot get input value of Struts 2 file selector with Angular

I am using Angular and I want to get access to the file input field's file name attributes and display it in another input box.
This is the file upload field:
<div class="btn btn-orange btn-file col-sm-3" >
<s:text name="expedientes.btn.seleccionar.fichero" />
<s:file name="form.filesUpload" multiple="multiple" ng-model="filesUploadModel" id="filesUploadId"/>
</div>
And the input box to show file name:
<input type="text" class="form-control"
id="fileNameId" name="fileName"
ng-model="fileNameModel" ng-disabled="true"
ng-init="" ng-bind="fileNameModel = filesUploadModel">
But the ng-bind is not working.
I also tried to define $watch for the file input field like this:
$scope.$watch(function() {
$scope.files = angular.element(document.querySelector('#filesUploadId'));
return files;
},
function(newValue, oldValue) {
$("#fileNameId").val(files.files[0].name);
});
to watch if the <input type="file" id="filesUploadId"> has changed, select this element and return it as files, and let the element with id fileNameId's value equals to files.files[0].name, because the file upload input has an attribute named files with all the files I upload, and their file names files[i].name.
But FF tells me files is undefined and no avail. It's not working.
Am I doing something wrong here? Please help and thanks!!
Edit: I am using this and no error, but no result either:
if (!angular.equals(document.getElementById("filesUploadId"), null)) {
$scope.$watch(function() {
var myFiles = document.getElementById("filesUploadId");
return myFiles;
},
function(newValue, oldValue) {
$( "#fileNameId" ).val(function(){
var result = null;
$(myFiles).each(function(){
result = name + this.attr(files).attr(name);
});
return result;
});
});
}
I solved it with pure JavaScript, enlighted by another question here:
AngularJs: How to check for changes in file input fields?
Actually, I find it impossible to use onchange() when the function I want to call is wrapped in angular module, except in the way in above answer:
onchange="angular.element(this).scope().setFileName()"
And in my script I only use pure JavaScript, except for the definition of the function:
angular.module('es.redfinanciera.app').controller('PanelMandarCorreoCtrl', function ($scope, $modalInstance) {
....(other functions)
$scope.setFileName = function() {
var result = "";
var adjuntos = document.getElementById("filesUploadId").files;
for (i = 0; i < adjuntos.length; i++){
result = result + adjuntos[i].name + "\r\n";
};
document.getElementById("fileNameId").value = result;
}
}
$scope.btnClean = function() {
document.getElementById("filesUploadId").value = "";
document.getElementById("fileNameId").value = "";
};
And in my jsp page, finally I have my file upload button and a clean button like this:
<div class="btn btn-orange btn-file col-sm-3" >
<s:text name="expedientes.btn.seleccionar.fichero" />
<s:file name="correoForm.filesUpload" id="filesUploadId" multiple="multiple" ng-model="filesUploadModel"
onchange="angular.element(this).scope().setFileName()"/>
</div>
<div class="col-sm-2">
<button class="btn btn-default btn-normal" type="button" ng-click="btnClean()">
<s:text name="expedientes.btn.quitar.fichero" />
</button>
</div>
I have a <textarea> to display all the file names:
<textarea class="form-control" ng-model="fileNameModel"
name="fileName" id="fileNameId"
ng-disabled="true"></textarea>
EDIT:
Clear button is not working in IE8 because it is not permitted in IE8 to set "" value to a file input field. My guess is, I can remove this file input field and copy a new one, with same style but no file is selected. But I have found a good question who has amounts of answers here:
clearing-input-type-file-using-jquery
Also, I heard that in IE8 onchange() event will not be triggered if you only select a file, you must add this.blur() after selecting a file. Regarding this issue, IE is following strictly the spec, but FF is not. But in my case, the event is actually triggered. Maybe because I am testing under IE 11 using Developing Tools' emulator for IE8.

AngularJs : Show block if $http response is success

I have 3 blocks that are showed based on a boolean.
<button ng-if="user.friendship == null" ng-click="requestFriendship(user.user.id)" class="button button-outline button-calm">
Invite as friend
</button>
<button ng-if="user.friendship == false" ng-click="removeFriendship(user.user.id)" class="button button-calm">
Friend request sent
</button>
<button ng-if="user.friendship == true" ng-click="removeFriendship(user.user.id)" class="button button-calm">
Unfriend
</button>
Well at first i don't know if it is the best solution to structure it that way, so do not hesitate to correct me here if i'm wrong.
then i have my function :
$scope.requestFriendship = function(id) {
$http.post(domain+'/api/friendship/request/'+id+'?access_token='+access_token.key).then(function(response){
// If success change button
}, function(error) {
console.log(error);
});
}
$scope.requestFriendship = function(id) {
// function
}
So based on the result (my api is returning success or failed, so if success), I need to Hide the previous button and change it to its new state.
So how can i hide and show buttons based on the answer of the API.
You would set $scope.user.friendship to the correct value to show and hide the relevant button:
This will show the unfriend button:
$scope.user.friendship = true;
This will show the Friend request sent button:
$scope.user.friendship = false;

Angular JS: Sort Grid Contents based on Dropdown Selection

I have a drop down button in my headerCellTemplate that looks like this
<div class="btn-group">
<button type="button" class="btn btn-default dropdown-toggle"
data-toggle="dropdown">
<b>Value</b><span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu" style="position:fixed;top:inherit;left:320px;">
<li>H</li>
<li>M</li>
<li>L</li>
</ul>
</div>
On click it is it suppose to sort the ng grid contents. I have written the code that can sort the grid contents from H-M-L. the function looks like this
var result = 1;
var test="HML";
function impSortFn(a, b) {
a=test.indexOf(a);
b=test.indexOf(b);
if (a == b) return 0;
if (a < b) return -1;
return result;
}
And the column Defs looks like this
columnDefs: [
{
field:'dt', displayName:'Date', width:'***',
cellFilter:'date:\"dd-MMM-yyyy\"', headerClass:'grad1',
cellTemplate:'<div><center>{{row.getProperty(\'dt\')|dateformat}}</center></div>'
},
{
field:'dt', displayName:'Time', width:'**',
cellFilter:'date:\"HH:mm:ss\"', headerClass:'grad1',
cellTemplate:'<div><center>{{row.getProperty(\'dt\')|timefilter}}</center></div>'
},
{
field:'rl', displayName:'Impact',width:'80px',
sortFn:impSortFn, headerClass:'grad1', headerCellTemplate:"impactHeader.html",
cellTemplate:'<div class="ngCellText" ng-class="col.colIndex()"><center><img ng-src="{{row.getProperty(\'rl\') | dateformat1}}"</img></center></div>'
}
]
Can any one please tell me how can I modify the function such that it returns only M values when M is selected from the drop down. and same for H and L.
In order to help in some way, i am posting a SO answer which is an alternative way of solving your problem.
It sure has better practices.
Have a look at this SO question.
In this question, ng-change is added to your element which is fully doable. According to a new value, you bind your grid again..
I used it in a project of mine. Here it is..
<input maxlength="34" type="text" ng-model="pollInfo.companyName" ng-change="updateIframe()" class="has-success form-control pull-left" placeholder="Anket Logo Yazısı" required id="companyName">
Ang get the change in controller
$scope.updateIframe = function() {
var data=new Object();
data.questions=$scope.questions;
data.pollInfo=$scope.pollInfo;
document.getElementById('iframe').contentWindow.updatedata(data);
};
You can do like this. In your method bind your grid. Again, this is not a best practice.
The best practice is that you do with $filter. But I need more info for your example to solve it.

Resources