ng-options with simple and complex array angularjs

In this article am going to explain how to use ng-options with simple and complex(array of objects) arrays

Assume you have following array

$scope.fruits = ['apple', 'banana', 'orange'];

If you want to show them in a dropdown using ng-options use the following code

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit as fruit for fruit in fruits"></select>

What you get in the resulting html is below

<option label="apple" value="string:apple">apple</option>
<option label="banana" value="string:banana">banana</option>
<option label="orange" value="string:orange">orange</option>

when you select any option the value gets selected is same as value shown in dropdown.

Use ng-options for array of objects:


If you have an array of objects like below

$scope.fruits = [{ id: 1, name: 'apple' }, { id: 2, name: 'banana' }, { id: 3, name: 'orange' }];

Use the following html to show them in dropdown

<select name="fruit" data-ng-model="selectedFruit"  data-ng-options="fruit.id as fruit.name for fruit in fruits"></select>

If you use the above html then in the dropdown it shows name of the fruit but internally it stores the id of the fruit when you select any value from dropdown. resulting html code looks like below

<option label="apple" value="number:1">apple</option>
<option label="banana" value="number:2">banana</option>
<option label="orange" value="number:3">orange</option>

When you click on dropdown it shows names of the fruits but when you select any option internally id of the object will be selected.

Check this Plunker for working example: ng-options with simple and complex arrays

This is how ng-options works with simple and complex arrays.

For more posts on angularjs visit: AngularJS

Read more...

How to undo or revert last commit(s) in Git

When you are working on git, it is possible that sometimes you may commit the wrong files or the code you committed some times needs to be reverted back.

There are many ways to revert the last commit or any specific commit in git. This SO post has many answers which explains how to revert a commit. But those are to be done in a command prompt which many are un-familiar with. So here am going to explain a easy way to revert a commit in git.

This can be achieved using Git Extensions. You can download it from here link1 or from here link2

Once you download Git Extensions, open it and select the repository where your source code exists.

Once this is done Git Extensions will show you all the commits. Select any specific commit and right click on it. Then select revert commit. Check the below screenshot for reference


Once you select the Revert Commit option, it opens a pop-up like below


Select "Automatically create a commit" if you want to directly commit the reverted changes or if you want to manually commit the reverted changes keep the box un-selected and click on "Revert this commit" button.

In this way you can easily revert any commit in git using Git Extensions.

For more useful articles on git visit this link: Git

Read more...

javascript: typeerror (intermediate value) is not a constructor

It's been long time since i have written a blog post as i am busy with the project deliverable. So, here i am with one more issue which i faced today while writing some basic javascript code.

The error is 

javascript: typeerror (intermediate value) is not a constructor

i came across this issue when i am returning some object from a function. Following is the similar javascript code that i have written

function buildContact() {
        return new { name: "John", mobile: "123-456-7890", country: "US" };
    } 

this is the javascript function which was showing the mentioned error. 

Cause:

I used new which can be only used with a Function as operand. In this case i am using object as a constructor which is actually not. 

Fix:

Here we have two options to fix this error. 

1. To return object directly as shown below
function buildContact() {
        return { name: "John", mobile: "123-456-7890", country: "US" };
    }      

2. Create a constructor and use the constructor to create new object as shown below

function Contact(name, mobile, country) {
        this.name = name;
        this.mobile = mobile;
        this.country = country;
    }

    function buildContact() {
        return new Contact("John", "123-456-7890", "US");
    }

in this case we are returning the new Contact() by using constructor. 

In this way you can fix the error javascript: typeerror (intermediate value) is not a constructor.

Please do comment if you know better way to fix this error. Thanks in advance. 

To know more common javascript errors visit: Javascript 


Read more...

Add new data row to data table Error: 'System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

We can not directly create a new DataRow() . When we try to do so, it throws this exception

System.Data.DataRow.DataRow(System.Data.DataRowBuilder)' is inaccessible due to its protection level

So in order to create new row in data table use the following method

DataTable table = new DataTable();
DataRow row = table.NewRow();
table.Rows.Add(row);
reference link : https://msdn.microsoft.com/fr-fr/library/9yfsd47w.aspx

In this way you can create a new data row and add it to data table.
Read more...

GIT Error : Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches

Today i came across a weird issue when i was trying to switch from my stage branch to main branch. Visual Studio was showing following error:

Cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details.


Visual studio states that we cannot switch to other branch when there are uncommitted changes. But in my case i don't have any pending/uncommitted changes. So, as suggested in the error message i had a look at the output window.
Following is the error shown in my output window.

It states that there are some changes in the ResourcesController.cs file. But i din't have any changes in that file.

Fix for the Issue:

So, to fix the issue i just deleted the above file and did undo (From Team Explorer) to add it again. I tried to switch branch now and it WORKED!!
This way i solved the error "cannot switch to master because there are uncommitted changes. Commit or undo your changes before you switch branches. See the Output window for details."
This is the solution which worked for me. But, it may not be the ideal solution and no guarantee that it works for all. But it worked for most of the people.

Update (Fix 2):

If the above solution did not work, try to change the branch directly from Git Extensions. Just open the Git Extensions and select the branch from branch drop down.


If you find any other way to fix this problem please do comment below so that it may help others.
Read more...