Posted in Angular, Code Snippets

AngularJS – UI-Router : Multiple states with same URL

Usually, with angular ui-router, we can have one url for a single state something like this:

...
.state('app', {
  url: "/home",
  templateUrl: "....",
  controller: "myController",
  roles: ['administrator','authenticated']
  ...
})
...

Continue reading “AngularJS – UI-Router : Multiple states with same URL”

Advertisements
Posted in PHP

PHP: Implode or Trim – Concatenating strings in a loop

There are bunch of questions circulating in Stack Overflow like this. People when trying to concatenate some strings with another string or a character, they simply concatenate with . inside the loop like this:

$values = [1, 2, 3];
$string = '';
foreach ($values as $value) {
$string .= $value . " AND ";
}
echo $string;

This leaves string like this:

1 AND 2 AND 3 AND

Then they come up with a question as how to remove the last AND.

Continue reading “PHP: Implode or Trim – Concatenating strings in a loop”

Posted in Angular

Make bootstrap button group to work with Angular

Many developers face the trouble to make the Twitter Bootstrap’s button group, toggle buttons to work with Angular. For instance, following is the code for button group – btn-group

<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary">
<input type="radio" name="options" id="option1" ng-model="radio.btnGroup" value="1"> Radio 1
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option2" ng-model="radio.btnGroup" value="2"> Radio 2
</label>
<label class="btn btn-primary">
<input type="radio" name="options" id="option3" ng-model="radio.btnGroup" value="3"> Radio 3
</label>
</div>

But ng-model doesn’t seem to work with this.

Continue reading “Make bootstrap button group to work with Angular”

Posted in Angular, Code Snippets

AngularJS: Recursive function with deferred promise

Deferred promise is a great way to handle async calls synchronously. When we use recursive function, we may wonder how to handle the defer variable. It is as simple as:

some_function = function(some_var1, some_var2, def) {
var deferred = def || $q.defer(); // Use def when it is recursive call
...
...
$http({
url: url,
method: "POST",
...
}).success(function(result) {
deferred.resolve(result);
}).error(function(result, status) {
...
some_function(some_var1, some_var2, deferred);
...
...
deferred.reject(result);
});
...
return deferred.promise;
};

Bold texts are those that needs to be added to handle deferred promise in recursive calls.

Continue reading “AngularJS: Recursive function with deferred promise”

Posted in Code Snippets, Javascript

Get filename from URL with extension

Get the filename from the URL or path using Javascript:

Consider the URL:

var url = "http://www.example.com/my_page.html"

Method 1:

url.split('/').pop()

Method 2: Regular Expression

url.replace(/^.*[\\\/]/, '')

Both method returns

my_page.html


But when URL is something like this with query parameters:

var url = "http://www.example.com/my_page.html?id=123&value=456"

Then above methods will return:

my_page.html?id=123&value=456

In order to remove query parameters from the URL, use:

url.split('/').pop().split('?')[0]

This again returns

​​​​my_page.html

Query parameters reside in next index.