When the user enters a specific expression, it gets translated to a button in the view.
Here is the code:
scope.buttonmaker = function(haystack) {
needle = /argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)/;
return $sce.trustAsHtml(haystack.replace(new RegExp(needle, 'gi'), function(match) {
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ', 2, 3, false)" class="btn btn-md btn-info"> ' + needle.exec(match)[2] + '</button>'
}));
};
The code works, as it should and it also captures more than one expression, that match the Regex and the buttons are made correctly.
However, when running a test, this error happens:
Error: Cannot supply flags when constructing one RegExp from another.
RegExp#[native code]
buttonmaker#http://127.0.0.1:46071/assets/application-a2e90460a4bfb22fd82a11fde4c3041112d7349933767571c5e4928ad1951bdb.js:56321:68
fn
Looking at this stackoverflow-question, I first thought, that I did make a spelling mistake, so I tried this:
needle = "argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)";
No errors occur, but the buttons weren't made.
Then, I tried this:
needle = /argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)/gi
return $sce.trustAsHtml(haystack.replace(needle, function(match) {
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ', 2, 3, false)" class="btn btn-md btn-info"> ' + needle.exec(match)[2] + '</button>'
}));
This error occurs:
Error: needle.exec(...) is null
After looking at this stackoverflow-question, I believe, I have to somehow iterate over my text, but I don't know how to do it in my case.
Can someone help me to correct the code, so it does work in tests too?
EDIT:
When I do this:
needle = /argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)/;
return $sce.trustAsHtml(haystack.replace(needle, function(match) {
return '<button ng-click="goToArgumentation(' + needle.exec(match)[1] + ', 2, 3, false)" class="btn btn-md btn-info"> ' + needle.exec(match)[2] + '</button>'
}));
No errors occur, but ONLY the first button gets made. This is obvious, because the global flag is missing.
And since the code works outside of tests, I think, the problem is more about the flags than the constructor.
I found a cheap fix, which requires not too much maintenance-work:
needle = /argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)/gi
needle2 = /argumentation-link_to\((\d+),\s([\w\sÀ-ž]+)\)/
return $sce.trustAsHtml(haystack.replace(needle, function(match) {
return '<button ng-click="goToArgumentation(' + needle2.exec(match)[1] + ', 2, 3, false)" class="btn btn-md btn-info"> ' + needle2.exec(match)[2] + '</button>'
}));
Related
I am trying to translate formlyjs required validation message in angularjs that's the code what I have tried but it doesn't translate or bring the translated string in "REQUIRED" I am using angular-translate.
Output i am getting UsernameREQUIRED instead of Username is required
I have looked at this question and answer over stackoverflow. it works with out to.label but i need to have this to.label concatenation.
Any help or suggestion please.
$translateProvider.translations("en", {
REQUIRED: "is required",
})
formlyValidationMessages.messages.required = 'to.label + "REQUIRED" | translate';
angular.module('templates/formly/validation-messages.html', []).run([
'$templateCache',
function ($templateCache) {
$templateCache.put('templates/formly/validation-messages.html',
'<formly-transclude></formly-transclude>' +
'<div class="validation-messages" ng-messages="(fc[0] || fc).$error" ng-if="(options.formControl[0] || options.formControl).$touched || (options.formControl[0] || options.formControl).$error.required || form.$submitted" ng-messages-multiple>' +
' <div class="label label-danger animate vanishIn enter-vanishIn exit-vanishOut" ng-message="{{::name}}" ng-repeat="(name, message) in ::options.validation.messages">' +
' {{message((fc[0] || fc).$viewValue, (fc[0] || fc).$modelValue, this)}}' +
' </div>' +
'</div>' +
'');
}
]);
Here is how I am adding fields.
$scope.addEmailField = function () { //Function to add new email field.
if (valid <= 1 && checkToDelete == 0) {
var mailTxtId = 'mail' + valid;
var mailModel = 'Contact_Email' + valid;
var hide = 'hide' + valid;
hide = false;
console.log(mailTxtId);
var emailDiv = angular.element(document.querySelector('#emailDiv'));
var element = $compile('<div id="' + mailTxtId + '" style="margin-left: -60px; width:200px; margin-top:15px"><input id= "' + mailModel + '" type = "text" class="form-control" ng-model="' + mailModel + '" ng-blur="validateEmailDynamic(' + valid + ')">' +
'<input id="' + valid + '" class="form-control" style="margin-left: 206px; width:54px; margin-top:-34px" type="button" value="-" ng-click="deleteField(' + valid + ')"><span ng-show ="' + hide + '" style="color:red">' +
'Invalid email</span></div>')($scope);
emailDiv.append(element);
valid = valid + 1;
}
};
But not getting the value of ng-model.
Store your input box in a directive's template. Then add ng-class that would determine whether it should show or not.
app.directive('inputBox', function(){
template:'<input ng-model="item">'
});
Usage in the html:
<div ng-class="{ input-box : triggerInputBox }"></div>
Controller:
$scope.triggerInputBox = true;
This is just one of many ways to accomplish this. But directives are very useful for dynamically showing templates.
I am using angular data table from below link
http://l-lin.github.io/angular-datatables/archives/#!/bindAngularDirective
I am using the defer rending concept here.
I have 2 button
Active (Show when user status is 1)
Inactive (Show when user status is 0)
As 2 way data binding not working here. I am unable to show Inactive button after clicking on active & viceversa
Below is my code
$scope.setCustomers = function (s, dto, dtc, f, c, h, t) {
s.dtInstance = {};
s.actions = {};
//dtOptions - Makes the ajax request to get the customer records, also builds the buttons to copy,csv,excel..etc
s.dtOptions = dto.fromSource('customers/showCustomers.json')
.withPaginationType('full_numbers')
.withDisplayLength(25)
.withOption('createdRow', createdRow)
.withDOM('<"html5buttons"B>lTfgitp').withDisplayLength(25);
// dtColumns - Builds the columns and renders the rows for each column
s.dtColumns = [
dtc.newColumn('name').withTitle('Name'),
dtc.newColumn('email').withTitle('Email'),
dtc.newColumn('phone').withTitle('Phone'),
dtc.newColumn('status').withTitle('Status')
.renderWith(function (data) {
return "<div style=text-align:center;>" + (data == 1 ? 'Accepted' : data == 2 ? 'Declined' : 'Pending') + "</div>";
}),
dtc.newColumn(null).withTitle('Actions').notSortable()
.renderWith(actionsHtml)
];
function createdRow(row, data, dataIndex) {
c(angular.element(row).contents())(s);
}
function actionsHtml(data, type, full, meta) {
s.actions[data.id] = data;
var actions = '<button ladda="loadingDemo' + data.customer_id + '" class="ladda-button ladda-button-demo btn btn-primary btn-xs" data-style="zoom-in" ng-click="resetPassword(actions[' + data.id + '])" uib-tooltip="Reset Password" tooltip-placement="top"><i class="fa fa-key"></i></button> '
+ '<i class="fa fa-money"></i></button> '
+ '<i class="fa fa-files-o"></i></button> '
+ '<i class="fa fa-eye"></i></button> '
+ '<i class="fa fa-pencil"></i></button> ';
if (data.is_active == 1) {
actions += '<button class="btn btn-success btn-xs" data-is_active="' + data.is_active + '" ng-click="setStatus(actions[' + data.id + '])" tooltip-placement="top" uib-tooltip="Inactive"><i class="fa fa-thumbs-up"></i></button> ';
} else {
actions += '<button class="btn btn-danger btn-xs" data-is_active="' + data.is_active + '" ng-click="setStatus(actions[' + data.id + '])" tooltip-placement="top" uib-tooltip="Active"><i class="fa fa-thumbs-down"></i></button> ';
}
return actions;
}
}
$scope.setCustomers($scope, DTOptionsBuilder, DTColumnBuilder, $filter, $compile, $http, toaster);
Thanks
How do I apply one time lazy binding to the following where the value is composed of an expression:
<span ng-bind="::firstname + ' ' + ::lastname"></span> // breaks
Why not composing them in your controller:
$scope.name = $scope.firstname + ' ' + $scope.lastname;
And then simply:
<span ng-bind="::name"></span>
<span ng-bind="::(firstname + ' ' + lastname)"></span>
Parens aren't necessary though.
I have this chart http://jsfiddle.net/Cp73s/2169/, I want to add different icons in the legend but I dont know How can do it.
labelFormatter: function () {
$scope.data = this.total;
console.log($scope.data);
return '<img src="http://icons.iconarchive.com/icons/visualpharm/must-have/256/Check-icon.png" width="15" height="15"></span>' + this.y + ' (' + this.percentage.toFixed(2) + '%) - ' + this.name;
},
There's quite an extensive list, so you'll need to manually map out what images you'd like to use for each item. I've created a JSFiddle which illustrates how to do this using a switch statement.
To make it a bit quicker I've used Font Awesome which i'd recommend looking in to:
labelFormatter: function () {
$scope.data = this.total;
var labelName = this.name,
icon = '';
switch(labelName){
case 'APP Android':
icon = 'android';
break;
case 'APP Ios':
icon = 'apple';
break;
default: // If no match is found, revert to a check icon
icon = 'check'
}
return '<i class="fa fa-' + icon + '"></i> ' + this.y + ' ('+ this.percentage.toFixed(2) +'%) - ' +this.name;
},