The $scope in Angular doesn't seem to be working as expected in the callback function, despite using $scope

I'm currently working on converting the JSFiddle found here to AngularJS: http://jsfiddle.net/danlec/nNesx/

Here is my attempt in JSFiddle: http://jsfiddle.net/leighboone/U3pVM/11279/

var onAuthorize = function () {
    updateLoggedIn();
    $scope.testname = "this works";
    Trello.members.get("me", function (member, $scope) {

        var test = member.fullName;
        console.log(test)
        $scope.$apply(function(){$scope.applytest = member.fullName;})
        $scope.member = member.fullName;

    });
};

Despite trying various solutions, I am facing difficulties assigning member.fullName to $scope.member. Even after using $scope.$apply(function(){}), an error keeps popping up:

undefined is not a function

It's frustrating as I'm running out of ideas on how to resolve this issue. :(

Answer №1

Check out the correct angular code snippet below To view the working fiddle, click here

HTML Connect To Trello

    </div>

    <div id="loggedin" ng-show="isLoggedIn">

        <div id="header">Logged in to as <span id="fullName">{{fullName}}</span>  <a ng-click="logout()" id="disconnect" href="#">Log Out</a>

        </div>
        <div id="output">
            <div ng-show="loadingCards">Loading Cards...</div>
            <div ng-repeat="card in cards">{{card}}<a href="{{card.url}}" target="trello" class="card">{{card.name}}</a>

            </div>
        </div>
    </div>
</div>

Javascript

function calendarCntrl($scope) {

$scope.cards = [];
$scope.fullName = '';
$scope.loadingCards = false;

$scope.logout = function () {
    Trello.deauthorize();
};
$scope.login = function () {
    alert('logging in');
    Trello.authorize({
        type: "popup",
        success: onAuthorize
    });
};

$scope.$watch(function () {
    return Trello.authorized();
}, function (val) {
    $scope.isLoggedIn = val;
});

function onAuthorize() {

    Trello.members.get("me", function (member) {
        $scope.fullName = member.fullName;

        $scope.loadingCards = true;
        // Output a list of all of the cards that the member 
        // is assigned to
        Trello.get("members/me/cards", function (cards) {

            $scope.cards.length = 0;
            angular.extend($scope.cards, cards);
            $scope.loadingCards = false;
        });
    });

};

Trello.authorize({
    interactive: false,
    success: onAuthorize
});

}

CSS

body {
font-family: arial;
font-size: 12px;
}
#loggedout {
    text-align: center;
    font-size: 20px;
    padding-top: 30px;
}

#header {
    padding: 4px;
    border-bottom: 1px solid #000;
    background: #eee;
}
#output {
    padding: 4px;
}
.card {
    display: block;
    padding: 2px;
}

Answer №2

After some consideration, I have come to the conclusion that setting it to the rootScope is the solution.

$rootScope.$apply(function(){$rootScope.applytest = member.fullName;})

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

Searching for TypeScript type definitions for the @Angular libraries within Angular 2

After updating my application to Angular2 v2.0.0-rc.1, I am encountering TypeScript compile errors and warnings when webpack bundles my application. These messages appear for any @angular packages referenced in my TypeScript source files: ERROR in ./src/a ...

What is the reason for instanceof Map returning false?

Utilizing the Draft.js plugin called Mention. When accessing editorState.content.entityMap.mention, I am able to retrieve the value by: mention.get('address') However, when I attempt to verify if it is a Map using: console.log('mention&a ...

What is the best way to include the API body in a GET request?

I'm facing an issue with passing parameters to the body instead of the query in my code. Here's what I have attempted: const fetchData = async () => { let response = await apiCall("URL" + { "companyArr": ["SBI Life Insurance C ...

React App folders are not being properly installed through NPX

Encountering an error message while attempting to use npx create-react-app Husna@LAPTOP-LPCC954R MINGW64 ~/Desktop/React GitHib Project (master) $ npx create-react-app github2020 Creating a new React app in C:\Users\Husna\Desktop\Reac ...

Next js is repeatedly calling a Firestore document in multiple instances during the fetching process

In my Next js 13 project, I am facing an issue while fetching a single document with an id from Firebase. Instead of returning just one read (which is expected since I'm fetching a single doc), it is returning multiple reads, sometimes ranging from 2 ...

Determining if an element corresponds to a By-object in Selenium using JavaScript

Is it possible to determine if a given WebElement matches a specific By object? The function should return either true or false. For example: foo(myWebElement, By.className("myClass myClass2")); foo(myWebElement, By.css("div")); foo(my ...

Retrieving Django view information using AJAX

Currently, I am in the process of developing an application that updates the main page's data via AJAX. After reading through a helpful response on this Stackoverflow post, I implemented AJAX to refresh my page. In addition to this, I have a specific ...

Running a shared function following every Promise resolution

Currently working on a nodejs project and utilizing bluebird.js for Promises. I'm looking to have a function executed after each .then() method in the chain. Does anyone know of a way or API within bluebird.js that supports this? Appreciate any guida ...

Click on the submenu to expand it, then simply select the desired option to navigate

I have created a toggle menu that displays a list of child elements when clicked, and hides them if clicked again. However, when a child element is clicked, I want it to navigate to the corresponding page. I am having trouble getting this functionality to ...

Encountering a 401 error message with a 'truncated server' response while using Google Apps Script

I'm currently working on a code snippet that looks like this. function method3() { var spreadsheetID = '1BGi80ZBoChrMXGOyCbu2pn0ptIL6uve2ib62gV-db_o'; var sheetName = 'Form Responses 1'; var queryColumnLetterStart = ...

The jQuery script is functioning flawlessly in one specific direction

Currently, I am utilizing a basic script to alter the color of elements based on the div being hovered over. While this works smoothly in one direction down the line, when trying to reverse order and backtrack, the colors do not function as intended. The ...

Using Three.js to rotate a camera-followed sphere in the scene

Currently, I'm developing a Three.js scene with a toy concept where I aim to track a sphere using a camera [demo]. However, I am facing an issue wherein I can't seem to get the sphere to "roll" without causing the camera to also rotate along with ...

The initial click may not gather all the information, but the subsequent click will capture all necessary data

Issue with button logging on second click instead of first, skipping object iteration function. I attempted using promises and async await on functions to solve this issue, but without success. // Button Code const btn = document.querySelector("button") ...

What's the deal with $routeProvider in angularJS?

I'm having some trouble with my simple web app that I'm trying to update using route provider. Everything works fine when I click on the list items on the page, but when I refresh the page, I get a 404 error. It seems like it's trying to acc ...

What is the best way to showcase the information stored in Firestore documents on HTML elements?

Currently in the process of designing a website to extract data from my firestore collection and exhibit each document alongside its corresponding fields. Below is the code snippet: <html> <!DOCTYPE html> <html lang="en"> <head> ...

Display the execution duration on an HTML document

Recently, I created a loan calculator using Javascript and now I'm contemplating on incorporating the time taken for the code to execute into the results. My intention is to have the execution time displayed on my HTML page, but I am unsure of how to ...

Use JavaScript to input array elements into a selection of cells in Excel

At this moment, the loop I am executing successfully retrieves the necessary values. However, I am facing challenges when it comes to inserting these array values into a range of cells. I keep encountering the following error message: Error message: "The ...

Javascript - Issue with Ajax causing additional commas in JSON responses

I'm attempting to send a request to a RESTful server using the HTTP module in Node.js. Due to the large response size (64 chunks, approximately 100kb), the HTTP module combines the chunks into a single string response like this: res.setEncoding(& ...

Guide to integrating Google Maps JS API into a React application without relying on third-party libraries

I'm currently grappling with the concept of integrating external APIs in React and am interested in using Google Maps' API to showcase a map within a child component. My goal is to gain insight into how this can be done without relying on any ext ...

ExpressJS: How to resolve the undefined error in Express session

After using express-generator to create my project, I installed the express-session package. Below is how express-session is ordered in my app: app.js var expressSession = require('express-session'); app.use(logger('dev')); app.use( ...