JavaScript

Showing posts with label angular. Show all posts
Showing posts with label angular. Show all posts

Tuesday, 13 October 2015

Create Cross Page Posting in web api through angular javascript

Develop you first web api application Using .Net IDE

   [ActionName("GetValues")]
        public IEnumerable<string> Get(string UserName, string Password)
        {
            return new string[] { "value1", "value2" };
        }

This API return array of string then we consume
using angular javascript

var phoneCateApp = angular.module('PhoneCatApp', ['ngRoute']);

phoneCateApp.controller('PhoneCateController', function ($scope, $http) {
   
    $http({
        url: 'http://localhost:52600/api/Values/GetValues',
        method: "GET",
     
        contentType: "application/json",
        headers : { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
  
        data: '{ UserName: fsdfsda, Password: fsdfsda }'
    }).success(function (data) { $scope.PhoneList = data; });

});

inject the ngRoute in angular javascript

then set the web.config web api

 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, OPTIONS, PUT, DELETE" />
      </customHeaders>
    </httpProtocol>

in <system.webserver>


Then your application call all your rest service successfully

Wednesday, 24 June 2015

Form Validation throgh Angular js

 i am searching on internet then found many missing thing on validation on form when you are beginner and learning in angular js
I am follow few step of validation of angular js then validate my form. validation script
   var d = angular.module('myapp', []);
    d.controller('HomeController', function($scope) {
      $scope.loginForm = {
        UserName: '',
        password: '',
        email: '',
        Gender: [{
          value: 0,
          text: '--select--'
        }, {
          value: 1,
          text: 'Male'
        }, {
          value: 2,
          text: 'female'
        }]
      }
      $scope.form = $scope.loginForm.Gender[0].value

      $scope.validate = function(model) {

        $scope.form1.$submitted = true;
        $scope.selectedGender = document.getElementById('ddlGender').value == '0'
        if ($scope.selectedGender) {
          return false;
        }
        if ($scope.form1.$valid) alert(1);

      }
      $scope.onSelectChange = function() {
        $scope.selectedGender = document.getElementById('ddlGender').value == '0'
      }

    })

Name This is required
Password This is required
Gender this is required