0

I've been following web tutorials to try to learn angularJS on a .NET MVC Application. All the tutorials seem to cover getting a list, getting an individual item etc.

What I want to do is allow the user to fill in an email address, I want to verify that email address against the database and return true or false if it existed. I'm then trying to put that value in the scope so I can do something in response to whether its true or false.

I'm using a single page app so this is the login html.

<form name="form" class="form-horizontal">
    <div class="control-group" ng-class="{error: form.ValidEmailAddress.$invalid}">
        <label class="control-label" for="ValidEmailAddress">Valid Email Address</label>
        <div class="controls">
            <input type="email" ng-model="item.ValidEmailAddress" id="ValidEmailAddress">
        </div>
    </div>
    <div class="form-actions">
        <button ng-click="login()" class="btn btn-primary">
            Go!
        </button>
        <label ng-if="user.isAuthorised">Authorised</label>
        <label ng-if="!user.isAuthorised">NotAuthorised</label>
    </div>
</form>

In my app.js file I declare a loginCtrl controller when the url was /login so that's all fine. The logic that I'm calling on my button click is this:

var LoginCtrl = function ($scope, $location, $http, AuthorisedUser) {

    $scope.login = function() {
           var isValidUser =  $http.get("/AuthorisedUser/IsValidUser/" + $scope.item.ValidEmailAddress);
           $scope.user.isAuthorised = isValidUser;
    } };

Which is then calling an MVC AuthorisedUserController class method:

public bool IsValidUser(string id)
{
    var list = ((IObjectContextAdapter)db).ObjectContext.CreateObjectSet<ApprovedUser>();
    var anyItems = list.Any(u => u.ValidEmailAddress == id);
    return anyItems;
}

So it vaguely seemed to be working when I put in a value like "aaa" into the textbox. But as soon I try putting in an email address the value is undefined. Maybe I'm supposed to be doing a post but the only thing I can successfully hit my .NET controller with is by using get.

I'm sure I'm missing fundamental knowledge and potentially tackling this in the wrong way.

In case it helps I've created a module and defined factories like this:

var EventsCalendarApp = angular.module("EventsCalendarApp", ["ngRoute", "ngResource"]).
    config(function ($routeProvider) {
        $routeProvider.
            when('/login', { controller: LoginCtrl, templateUrl: 'login.html', login: true }).
            otherwise({ redirectTo: '/' });
    });


EventsCalendarApp.factory('AuthorisedUser', function ($resource) {
    return $resource('/api/AuthorisedUser/:id', { id: '@id' }, { isValidUser: { method: 'GET' }  });
});

One of my questions is - should I be accessing the controller method using the $http object, or is there a way of using my factory declaration so that I can go something like:

AuthorisedUser.IsValidUser($scope.item.validEmailAddress)

I know in the tutorial I was following I could do stuff like:

 CalendarEvent.save()

to be able to call a CalendarEventController post method.

Jen
  • 1,964
  • 9
  • 33
  • 59
  • so "aaa" works but email does not? – Jorg Jul 02 '14 at 06:30
  • Correct (I'd originally had my input type of text, and tried changing it to email in case that helped (which I noticed I copied above), but it started passing through undefined instead of the string from the input box. – Jen Jul 02 '14 at 06:37
  • `@` can't be used in a url, and a get request uses url parameters.`@` is a reserved keyword. maybe you can try encoding / decoding it. – Jorg Jul 02 '14 at 06:38
  • I would have thought angular would take care of that if you use the input type of email. It's been too long since I've done web development so I'm not sure how to handle encoding it. – Jen Jul 02 '14 at 06:53
  • Let's take a step back then... Can you log the values to the console and see? Do the URLs look ok in the dev console's network tab? – Jorg Jul 02 '14 at 07:17
  • So when I pass through an email address in the input box I'm getting a 404. It's saying: http://localhost:51167/AuthorisedUser/IsValidUser/aaa@test.com is not found. Is that still because an email address needs to be encoded? – Jen Jul 03 '14 at 00:13
  • Found the answer over here http://stackoverflow.com/questions/4307358/how-can-i-accept-email-address-as-a-route-value By adding a trailing slash the error stopped occurring! Thanks for your help though :) @Jorg – Jen Jul 03 '14 at 00:19

1 Answers1

1

What i think is, your get() function will return a promise. and you can't assign promise like this. so better try this approch once. I hope, it'd work. if not please let me know...

here I assume your first,second and third snippet of code works fine...

$http.get("/AuthorisedUser/IsValidUser/" + $scope.item.ValidEmailAddress).success(function (result, status) {

        var isValidUser=result;
        $scope.user.isAuthorised = isValidUser;
        $scope.$apply();
    }).error(function (result, status) {
        //put some error msg
    });
micronyks
  • 54,797
  • 15
  • 112
  • 146
  • Sorry tried to post a comment last night and the work internet decided to go into limbo! This solution is probably in the direction I need but still have the issue about the mvc controller not liking the email address passed to it. Based on what Jorg has said I'll probably need to figure out encoding it or something. Thanks. – Jen Jul 02 '14 at 23:15
  • To fix my error I had to add a trailing slash so ("/AuthorisedUser/IsValidUser/" + $scope.item.ValidEmailAddress + "/") , after that stopped getting the 404 error. But your answer got me better handling success and failure of the async call. Thanks :) – Jen Jul 03 '14 at 00:21
  • oh finally happy that it helped you. If, still there is an issue you can ask. better solution will be provided by anybody. – micronyks Jul 03 '14 at 05:13