0

My controller name is ViewPostOnThreadCntrl and the id of the controller div is ViewPostOnThreadCntr. When I call ng-init function in JavaScript then the following error is displayed:

Uncaught TypeError: Cannot read property 'GetPostReplyByThreadId' of undefined

View Code

<section class="trdsec" ng-controller="ViewPostOnThreadCntrl" id="ViewPostOnThreadCntrl" ng-cloak>
  <div ng-init="GetPostReplyByThreadId()">
  </div>
</section>

JavaScript angular.element(document.getElementById('#ViewPostOnThreadCntrl')).scope().GetPostReplyByThreadId();

Controller

app.controller("ViewPostOnThreadCntrl", function ($scope, angularService) {

    $scope.GetPostReplyByThreadId = function () {
        id = getUrlParameter('id');
        if (id != '' && id > 0) {
            var getData = angularService.GetPostReplyByThreadId(id);
            getData.then(function (InnerDetail) {
                $scope.InnerDetails = InnerDetail.data;
                if (InnerDetail.data.CategoryId < 1)
                {
                    window.location='/Home/Index'
                }
            }, function () {
            });
        }
        else
        {
            window.location='/Home/Index'
        }
    }

Javascript:

success: function (result) {
            isSuccess = result;
            if (result > 0) {
                angular.element('#ViewPostOnThreadCntrl').scope().GetPostReplyByThreadId();
                $(e).closest("form").find("#PostText").val("");
                if (ID == 0) {
                    alert("Post Replied successfully.");
                }
                else {
                    alert("Replied successfully.");
                }
                $('html, body').animate({ scrollTop: $(ID > 0 ? "#div-" + ID + "" : ".footer").offset().top }, 2000);
                if (ID == 0)
                    $('#ShowDiv').css({ 'display': 'none' });
            }
            else {
                alert("Post Replied Unsuccessfully.");

            }

        },
        error: function (result) {
            alert("Post Replied Unsuccessfully.");
        }

    });
2
  • when i use "angular.element('#ViewPostOnThreadCntrl').scope().GetPostReplyByThreadId();" then "Uncaught Error: [jqLite:nosel] errors.angularjs.org/1.4.6/jqLite/nosel" error occur Commented Mar 28, 2017 at 10:41
  • 1
    Where is that JavaScript? Show your controller code in full. Commented Mar 28, 2017 at 17:45

1 Answer 1

0

Inside your angular controller define a function as follows

$scope.GetPostReplyByThreadId = function(){

// your logic here...

}

delete angular.element(...) line of code

Sign up to request clarification or add additional context in comments.

2 Comments

$scope.GetPostReplyByThreadId is already Define in Angular. Error At Line angular.element('#ViewPostOnThreadCntrl').scope().GetPostReplyByThreadId(); "Uncaught Error: [jqLite:nosel] errors.angularjs.org/1.4.6/jqLite/nosel"; error occur
but When i use angular.element(document.getElementById('#ViewPostOnThreadCntrl')).scope().GetPostReplyByThreadId(); then following error occur "Uncaught TypeError: Cannot read property 'GetPostReplyByThreadId' of undefined"

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.