0

I am new to AngularJs, so appologise if I ask something silly.

I am using angularSlideOutPanel to populate a popup by calling a controller "TeacherDetailsController". the controller TeacherDetailsController further calls a AnularJS Service that calls an MVC Controller's Action method to get data in Json format.

The problem I am getting is my TeacherDetailsController gets fired but it never hits the line

 $scope.GetTeacherInfo = function () 

I am not sure how to call the GetTeacherInfo Service.

Below is my relevant code:

Controller

    var app = angular.module('myFormApp', [
  'angular-slideout-panel'
])
.controller('HomeController', function ($scope, angularSlideOutPanel, $http, $location, $window) {
    getallData();


//******=========Get All Teachers=========******  
    function getallData() {
        $http({
            method: 'GET',
            url: '/Home/GetAllData'
        }).then(function successCallback(response) {
            // this callback will be called asynchronously
            // when the response is available
            $scope.ListTeachers = response.data;
        }, function errorCallback(response) {
            // called asynchronously if an error occurs
            // or server returns response with an error status.
            $scope.errors = [];
            $scope.message = 'Unexpected Error while saving data!!';
            console.log($scope.message);
        });
    };


    //******=========Get Single Customer=========******

    $scope.getTeacher = function (teacherModel) {

        $http({
            method: 'GET',
            url: ('/Home/GetbyID/' + teacherModel.TeacherNo),
            params: { "TeacherNo": teacherModel.TeacherNo }
        }).then(function successCallback(response) {
            $scope.teacherModel =response.data;
            getallData();
        }, function errorCallback(response) {
            $scope.message = 'Unexpected Error while loading data!!';
            $scope.result = "color-red";
            console.log($scope.message);
        });
    };

    //******=========slide Customer popup =========******
    $scope.openPanelRight = function (teacherModel) {
        var panelInstance2 = angularSlideOutPanel.open({
            templateUrl: '/Templates/teacherDetail.html',
            openOn: 'right',
            controller: 'TeacherDetailsController',
            resolve: {
                Teacher: [
                  function () {
                      return teacherModel;
                  }
                ]
            }
        });
    };

})
.controller('TeacherDetailsController', ['$scope', 'TeacherService', 'Teacher', function ($scope, TeacherService, Teacher) {

    $scope.Teacher = Teacher;
       $scope.closePanel = function () {
           $scope.$panelInstance.close('this is from the controller!!');
       };

       $scope.GetTeacherInfo = function () {
           var TeacherInfo = TeacherService.GetTeacherInfo(Teacher.TeacherNo);
           TeacherInfo.then(function (ord) {
               $scope.Teacher = ord.data;
           }, function () {
               genericService.warningNotify("Error in getting Teacher Info");
           });
       }
   }])

.config(function ($locationProvider) {
    $locationProvider.html5Mode(true);
});

Service

app.service("TeacherService", ['$http', '$location', function ($http, $location) {

    this.GetTeacherInfo = function (TeacherNo) {
        var response = $http({
            method: "get",
            url: "/Home/GetbyID?TeacherNo=" + TeacherNo
        });
        return response;
    }


}]);

MVC Controller

public class HomeController : Controller
{
    [HttpGet]
    public JsonResult GetbyID(int TeacherNo)
    {
        return Json(Workflow.Teacher.GetTeacher(TeacherNo), JsonRequestBehavior.AllowGet);
    }

}

Calling HTML

    <a href="#" ng-click="openPanelRight(teacherModel)" title="Edit Record">
        <div class="col-xs-12 col-sm-9">
            <span class="name">{{teacherModel.TeaFName}} {{teacherModel.TeaSName}}</span><br />
            <span class="fa fa-comments text-muted c-info" data-toggle="tooltip" title="{{teacherModel.TeaEmail}}"></span>
            <span class="visible-xs"> <span class="text-muted">{{teacherModel.TeaEmail}}</span><br /></span>
        </div>
    </a>
5
  • You need to invoke the method i.e. $scope.GetTeacherInfo(), Where are you calling it? Commented May 12, 2017 at 11:48
  • Where have you called $scope.GetTeacherInfo function?? Commented May 12, 2017 at 11:52
  • I have added the calling html markup, I am calling the openPanelRight through ng-click, Would $scope.openPanelRight is not suppose to call the controller: 'TeacherDetailsController'? Commented May 12, 2017 at 11:54
  • openPanelRight() method has nothing to do with $scope.GetTeacherInfo(), where you specifically call $scope.GetTeacherInfo() ? Commented May 12, 2017 at 12:01
  • I am not calling $scope.GetTeacherInfo() from anywhere. Whats the best place to call this ? Commented May 12, 2017 at 12:03

2 Answers 2

1

call GetTeacherInfo() methos inside your TeacherDetailsController if you instantly need data on controller load. like below :

.controller('TeacherDetailsController', ['$scope', 'TeacherService', 'Teacher', function ($scope, TeacherService, Teacher) {

       $scope.Teacher = Teacher;
       $scope.closePanel = function () {
           $scope.$panelInstance.close('this is from the controller!!');
       };

       $scope.GetTeacherInfo = function () {
           var TeacherInfo = TeacherService.GetTeacherInfo(Teacher.TeacherNo);
           TeacherInfo.then(function (ord) {
               $scope.Teacher = ord.data;
           }, function () {
               genericService.warningNotify("Error in getting Teacher Info");
           });
       }

       $scope.GetTeacherInfo();  //call here
   }])
Sign up to request clarification or add additional context in comments.

1 Comment

@ShK : no problem.. glad to help you. :)
0

Try init function.. And call it. Try like below.

.controller('TeacherDetailsController', ['$scope', 'TeacherService', 'Teacher', function ($scope, TeacherService, Teacher) {

    function init(){
     $scope.Teacher = Teacher;
    $scope.GetTeacherInfo();
    }

 $scope.closePanel = function () {
           $scope.$panelInstance.close('this is from the controller!!');
       };

       $scope.GetTeacherInfo = function () {
           var TeacherInfo = TeacherService.GetTeacherInfo(Teacher.TeacherNo);
           TeacherInfo.then(function (ord) {
               $scope.Teacher = ord.data;
           }, function () {
               genericService.warningNotify("Error in getting Teacher Info");
           });
       }

init();

   }])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.