issue with angular directive not properly binding data

I am curious about the following code:

HTML:

<div class="overflow-hidden ag-center" world-data info="target"></div>

js:

.directive('worldData', ['$interval', function($interval) {
    return {
        scope: {
            chart: '=info'
        },

        template: '<div>{{chart.aaa}}</div>',

        link: function($scope, element, attrs) {

            $scope.target = {'aaa': 'aaa'};

            aaa = $scope.chart;
        }
    }
}])

I find it interesting that the chart value is undefined and the template has no value. However, when I declare $scope.target within the controller, the code works perfectly. Can you explain why this is happening?

Answer №1

Following is the general pattern to be followed:

.controller('myController', function($scope){
    $scope.target = {'aaa': 'aaa'}; //Ideally, this data should be fetched using a different method such as $http.
})

.directive('worldData', [function() {
    return {
        scope: {
            chart: '=info'
        },

        template: '<div>{{chart.aaa}}</div>'
     }
}])

--

<div ng-controller="myController">
    <div class="overflow-hidden ag-center" world-data info="target"></div>
</div>

Another approach could be for the directive to handle fetching the data instead of passing it in. This option should only be considered if the data is not needed in multiple locations.

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

The watermark feature in HTML may not appear when printed on every page

I'm facing an issue with adding a watermark style. The watermark displays only on the first page when attempting to print in Chrome. It works perfectly in Firefox and IE. <style type="text/css"> #watermark { color: #d0d0d0; font-size: 90pt; ...

What is the best way to select the destination folder for output in Webpack?

Whenever I run webpack using "webpack --mode development", it generates a dist folder and places the bundle.js file inside it. My aim is to have it created and placed in the same directory instead. How can I achieve this? module.exports = { entry: " ...

Maximizing the potential of selectors in jquery.min.js

Can anyone explain what the purpose of selectors = {cacheLength: is in the jquery.min.js file? Additionally, I'd like to know if it is feasible to modify the value of cacheLength using jQuery. =ga.getText=function(a){var b,c="",d=0,f=a.nodeT ...

The Mongoose .lt method is unable to process the post value

I came across this interesting Mongoose function snippet: exports.readSign = function(req, res) { if (req.user.roles.indexOf('admin') === 1) { Timesheet.find() .where('projectId').equals(req.params.projectId) ...

What causes the Angular child component (navbar) to no longer refresh the view after a route change?

Hello everyone, I'm excited to ask my first question here. Currently, I am working on developing a social network using the MEAN stack and socket.io. One of the challenges I am facing is displaying the number of unread notifications and messages next ...

Custom cellRenderer prevents Ag Grid's autoHeight and wrapText features from functioning properly

I've been attempting to adjust the formatting of a long cell value by wrapping the text. According to the documentation, setting autoHeight=true and wrapText=true works fine without any cellRenderer components. However, when using a cellRendererFramew ...

Having trouble passing data from router to View with EJS in Express. Despite trying, still encountering ReferenceError message

I encountered an issue when trying to display form errors to the user. I attempted to pass the errors from the router to my view file. Error Message ReferenceError: e:\2016\passport\views\register.ejs:38 36| 37| <div ...

Display sub navigation when clicked in WordPress

I currently have the default wordpress menu setup to display sub navigation links on hover, but I am interested in changing it so that the sub navigation only appears when the user clicks on the parent link. You can view my menu here https://jsfiddle.net/f ...

Avoiding the automatic alphabetical ordering of JSON objects in ReactJS

I'm facing a challenge where JSON is automatically sorting stored data causing a lot of issues for me. Here's the code snippet I'm working with: <FormControl component="fieldset"> <FormLabel component="legend">Days of ...

What is the best way to simultaneously update the model and view values in an Angular directive?

My apologies if directives are not my strong suit! Within this simple attribute-only directive, the primary goal is to automatically convert a string in a field to an HH:mm format upon blurring the field. The directive code is as follows: (function () { ...

Experiencing difficulties incorporating $cordovaSocialSharing into the controller due to an error

I have successfully developed a hybrid app using the Ionic framework. To enable social sharing functionality within my app, I integrated the cordovaSocialShare plugin from NgCordova. After executing the command cordova plugin add <a href="https://github ...

Add a variable from a callback function in AJAX's success response

Is there a way to effectively utilize the variable in the appended message of the AJAX success call? http://codepen.io/anon/pen/fdBvn data['name'] = $('#goofy').val(); $.ajax({ url: '/api/1/email/test/', data: data, type ...

Tips for integrating Google Analytics with React

In my react application, I have various pages: Main Service Contact Profile (private) etc.. To monitor user activity using Google Analytics, I came across the react-ga library which serves the purpose well. However, I find myself having to initialize G ...

SQL query using Ajax

I'm currently working on an Ajax call using a query string, but I've hit a roadblock midway through. My goal is to update a SQL table with the JavaScript sortable function. After an item has been moved up or down, I want to pass it through Ajax a ...

Refine JSON data by selecting only distinct key/value pairs

My JSON object has the following structure: var theSchools = { Bradley University: "bru", Knox College: "knox", Southern Illinois University Edwardsville: "siue",… } I am trying to find a way to retrieve the school name (key) based on the schoo ...

Disable the smooth scroll animation when transitioning between pages using the Link component in NextJS

Not sure if this is a new feature of Next.js, as I didn't encounter this issue in my previous Next.js project. When I reach the bottom of a page and try to navigate to another page, a scroll-to-top animation appears on the destination page. I want to ...

What could be the reason for angularjs not functioning as expected?

After attempting to create a basic JavaScript unit test using angular.js, I encountered failure without understanding the reason behind it. test.html <html> <head> <script src="angular-1.2.21/angular-scenario.js" ng-autotest></ ...

Canvas 'toDataURL' Execution Error Occurs with Angular Dynamic Routing

For my Web application, I am using AngularJS on top of Node/Express. One of the routes in my application has an HTML5 canvas which I convert to an image. When I navigate to that route directly with this code, there are no problems with converting the canva ...

What's the best way to add a timestamp and class name to Angular's $log for an enhanced logging experience?

Is there a way to customize Angular logs by adding a timestamp and classname? For example: $log.info('this log entry came from FooBar'); "9:37:18 pm, FooBar: this log entry came from FooBar" I've come across various examples online that ...

Utilizing a form on numerous occasions prior to its submission

As a newcomer to JavaScript, I am exploring the best approach for a specific task. The task involves a form with checkboxes representing different music styles and a selector for names of people. The goal is to allow users to select music styles for mult ...