Unlocking the angular init function in JavaScript with the power of angular.element

My controller's name is DisplayThreadCtrl and the id of the controller div is DisplayThreadContainer. When I invoke the ng-init function in JavaScript, I encounter the following error:

Uncaught TypeError: Cannot read property 'GetPostReplyByThreadId' of undefined

View Code

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

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

Controller

app.controller("DisplayThreadCtrl", 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.");
        }

    });

Answer №1

In your Angular controller, create a function like this


$scope.fetchPostReplies = function(){
  // Add your logic here...
}

Remove the angular.element(...) line of code

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error encountered: The Bootstrap modal() function is showing as undefined when using npm modules

Every time I attempt to call $("#myDiv").modal(), an error occurs. The error message reads: Uncaught TypeError: undefined is not a function This error has popped up in different scenarios, with various parameters being passed to modal(). Many solutions o ...

Generating an array of keys from duplicated values in Typescript

My data is structured in the following array format: { itemTitle: 'value example', itemType: 'value example', itemDescription: 'value example', itemFamily: 'Asset', }, { itemTitle: 'val ...

Creating a button that integrates an image within a single div element

Currently, I understand the code: $('#leftDev').css("background-image", "url(img/1.png) "); will set the entire background of my leftDiv to display "1.png". However, I would like to position this image as a regular picture within the div and no ...

Encountering an issue upon pressing the 'calculate' button

Currently, I am delving into express node.js and attempting to execute a straightforward calculator code. However, whenever I click on the button, I do not receive any response, and there are no errors in my code either. I find myself a bit lost in figurin ...

Enhance your Three.js experience with antialiasing, top-notch rendering, and stunning FX

I've been working on a three.js project that involves 3D models, a ground, and a grid. The 3D models are outlined using outlinePass (). With TransformControl (), I can move objects, and OrbitControls () allows me to change the camera position. Howev ...

Enhance your google charts with easy updates through mouseover interaction

I am currently utilizing Google charts to dynamically display some data. My goal is to have two separate charts shown. The first chart will display f(x) vs. x, while the second chart will display g(x,y) vs. y (with a fixed x value). When hovering over a d ...

JavaScript data structure similar to ListOrderedMap/ArrayMap

Does anyone know of a JavaScript data structure that functions similarly to ListOrderedMap? I need it to have the capability to add an object at a specific index, retrieve the index for a given object, and lookup an object by its id. Unfortunately, all t ...

Most effective method for transferring a parameter to a new controller during navigation

How can I effectively pass a parameter when transitioning from one controller to another (parallel) one? I appreciate your assistance ...

Cypress: Verifying the JSON Data

As someone who is relatively new to cypress.io, I am currently working on a post call that returns a json object. Following the documentation provided by cypress.io, I have implemented the code snippet below: expect(response.body).to.have.property('c ...

Issue with dropdown selection functionality in Angular.js not functioning properly during page load

I have developed a small Angular.js app that handles room booking information. The user can input details about the booking, which are then stored in a database and retrieved when necessary. One issue I am facing is with the Angular select option for load ...

Obtaining the 3D point coordinates from UV coordinates on a 3D plane object using Three.js

I am in the process of creating basic data visualizations using Three.js as my tool of choice. I have a series of PlaneGeometry meshes to which I am applying a transparent texture dynamically generated with red squares drawn at varying opacity levels. My g ...

What is the process for adding an event listener in React?

Currently, I am working on a chat application that involves both users and agents. I am facing an issue where I need to retrieve messages when the agent responds using a separate Rainbow UI. According to the documentation, this can only be achieved using a ...

Is there an alternative solution to the issue of the jQuery resize() event not triggering when an input is

Despite the fact that the jQuery event resize() should trigger when the width of an input box changes, it seems to be malfunctioning. As per the jQuery API documentation, the resize event is supposed to go to the (window) handler. So my query here is, what ...

How can I change the attributes of icon().abstract.children[0] in the fontawesome-svg-core api?

The issue at hand: The icon() function within the fontawesome-svg-core API is setting default properties for SVG children elements that require custom modifications. My objective: The outcome of the icon() method is an object with an "html" property, co ...

Error in the syntax of node.js and node-postgres

I recently completed the installation of node-postgres by executing npm install pg . Within my source code, I included var pg = require('pg"); and upon running it, I encountered the following error: Error: Module 'pg' not found at Fun ...

creating arguments that have default values when using the new Function() syntax

How can I set default values for arguments when using the new Function method? let fileName = "fileone.js", args = [], options = {}; let ls = new Function('fileName', 'args', 'options', 'somefunctionorValue&apos ...

How can we enforce that the input consists of digits first, followed by a space, and then

Can regex (with javascript possibly) be used to mandate numbers, followed by a space, and then letters? I'm a bit lost on where to start with this... I understand that using an HTML5 pattern would work for this... [0-9]+[ ][a-zA-Z0-9]+ However, I ...

Data loss occurs when the function malfunctions

Currently, I am working with Angular version 11. In my project, I am utilizing a function from a service to fetch data from an API and display it in a table that I created using the ng generate @angular/material:table command. Client Model export interfac ...

Child component in Angular failing to trigger change event in list

In my current project, I have a parent component responsible for making an AJAX call to fetch data from the backend to populate a grid. To render the grid data, I created a child component where I pass the list as an in-binding. However, I noticed that whe ...

Converting a String to an Integer in JavaScript

I'm having trouble printing the sum in this code // 1. create variables // 2. input numbers into variables [but they are strings] // 3. unable to print the sum // Variables let num = [""]; let num22 = [""]; // Add new number to num ...