0

I'm new to web development and I'm currently trying to separate my UI by keeping my UI in Angular.js using views and routing and controllers and services. Whereas, I'm using a Web API to fetch the data from the database.

I'm trying to figure out how to get the views to work using Angular.js instead of using Asp.net's razor MVC. It works fine if I use the default routing from the ASP.net MVC web app and api but won't work if I use Angular.js routing.

  1. I've separated my Views in a folder and created the views I want
  2. I have set up a module with the routing for the separate views as well as the controller associated with them.
  3. I have included the app as ng-app directive in the html file.

Index.cshtml:

<div ng-app="myApp">
    <div ng-controller="StudentCtrl">

            <tbody>
                <tr ng-repeat="dataModel in students">
                    <td style="display:none">{{dataModel.StudentID}}</td>
                    <td> {{dataModel.FirstName}}</td>
                    <td> {{dataModel.LastName}}</td>
                    <td>{{dataModel.Email}}</td>
                    <td>{{dataModel.Address}}</td>
                    <td style="text-align:right; color:white">
                        <span>
                            <span id="save" class="btn btn-primary margin-right-btn"
                                  ng-click="GetStudentByID(dataModel)">
                                Edit
                            </span>
                        </span>
                    </td>
                    <td style="text-align:right; color:white">
                        <span>
                            <span id="save" class="btn btn-danger margin-right-btn"
                                  ng-click="DeleteStudent(dataModel)">
                                Delete
                            </span>
                        </span>
                    </td>
                </tr>
            </tbody>
            <tfoot></tfoot>
        </table>


    </div>
</div>

<script src="~/Scripts/angular.min.js"></script>
<script src="~/ScriptsNg/Module/sepView.js"></script>
<script src="~/ScriptsNg/Controller/StudentCtrl.js"></script>
<script src="~/ScriptsNg/Service/CrudService.js"></script>

Controller in Angular.js: (StudentCtrl.js):

sepView.controller('StudentCtrl', ['$scope', 'CrudService',
    function ($scope, CrudService) {

        // Base Url 
        var baseUrl = '/api/StudentAPI/';
        $scope.btnText = "Save";
        $scope.studentID = 0;
        $scope.SaveUpdate = function () {
            var student = {
                FirstName: $scope.firstName,
                LastName: $scope.lasttName,
                Email: $scope.email,
                Address: $scope.address,
                StudentID: $scope.studentID
            }
            if ($scope.btnText == "Save") {
                var apiRoute = baseUrl + 'SaveStudent/';
                var saveStudent = CrudService.post(apiRoute, student);
                //Callback to check what kind of response we received:

                saveStudent.then(function (response) {
                    if (response.data != "") {
                        alert("Data Saved Successfully");
                        $scope.GetStudents();
                        $scope.Clear();

                    } else {
                        alert("Some error");
                    }

                }, function (error) {
                    console.log("Error: " + error);
                });
            }
            else {
                var apiRoute = baseUrl + 'UpdateStudent/';
                var UpdateStudent = CrudService.put(apiRoute, student);
                UpdateStudent.then(function (response) {
                    if (response.data != "") {
                        alert("Data Updated Successfully");
                        $scope.GetStudents();
                        $scope.Clear();

                    } else {
                        alert("Some error");
                    }

                }, function (error) {
                    console.log("Error: " + error);
                });
            }
        }

app module (sepView.js):

var sepView = angular.module('myApp', ['ngRoute']);

sepView.config(function
    ($routeProvider) {
    $routeProvider
        .when('/', {
            controller:
                'StudentCtrl',
            templateUrl:
                'Views/Student/Index.html'
        })
        .otherwise({
            redirectTo: '/'
        });
});



Service to retrieve data from Api:

sepView.service('CrudService', function ($http) {

    var urlGet = '';
    this.post = function (apiRoute, Model) {
        var request = $http({
            method: "post",
            url: apiRoute,
            data: Model
        });
        return request;
    }
    this.put = function (apiRoute, Model) {
        var request = $http({
            method: "put",
            url: apiRoute,
            data: Model
        });
        return request;
    }

This is what the MVC generated controller looks like:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CRUDOperations.Controllers
{
    public class StudentController : Controller
    {
        // GET: Student
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult viewAllStudents() {
            return View();
        }
    }
}

This is what is happening:

https://i.sstatic.net/43a17.jpg

As you can see, angular can't access what it needs to populate the view. It was working fine when I was using Razor pages and MVC default routing.

5
  • 1
    Is there anything logged in browser console? and btw, if you're really new to web development, I highly encourage you to explore Angular 8 with asp net core api (instead of exploring angular JS, which is a few years old already and (probably) no company would consider angular JS over angular or similar for new projects)
    – AD8
    Commented Aug 7, 2019 at 1:10
  • 1
    When the double curly brackets ({{ }}) show it means AngularJS did not compile them. There are usually error messages in the Developer Console. Read AngularJS Error Reference - $injector modulerr
    – georgeawg
    Commented Aug 7, 2019 at 4:00
  • @AD8 Yes I checked the developer tools and it was what georgeawg mentioned. I was able to fix it by adding the script reference. Was a long day, somehow forgot that haha! Everything is working now, thank you!
    – tt1997
    Commented Aug 7, 2019 at 17:19
  • I do have a followup question. @georgeawg how does the Angular.js controller interact with the MVC controller which contains the return view() method. I'm kind of confused in making this link..
    – tt1997
    Commented Aug 7, 2019 at 17:21
  • @tt1997 I believe they won't interact directly. But what you could do is design angular JS controller for each view and have it look after what needs to be managed on that page. And you could of course utilize angular JS services to perform some actions that are common across multiple angular JS controllers. But again, I really encourage you to do some research about angular JS vs other frameworks. and also fyi, angular JS and angular (2+) have nothing in common, so if you're looking at angular JS to prepare base for angular - you don't need that. :)
    – AD8
    Commented Aug 7, 2019 at 23:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.