My directive is not being loaded in Angular

Recently, I have started using Angular but encountered an issue with loading my directive. I am looking to load my directive immediately upon page load.

Where should I load the data-show directive?

        <div class="row">
        <div class="col-md-12">
            <article class="row" ng-controller="DataCtrl">
                <input type="button" ng-click="getDataList()" >
                <h1>Some Content Here</h1>
                <ul id="home" bread-crumbs></ul>
                <ul class="thumbnails">
                    <li ng-repeat="data in list" class="col-md-5">
                        <show-data data="data"/>
                    </li>
                </ul>
            </article>
        </div>
    </div>

ShowData directive:

app.directive('showData', function () {
return{
    restrict: 'E',
    replace:true,
    templateUrl: 'views/directives/datas.directive.html',
    scope: {
        data: "="
    },
    controller:'DataCtrl'

}

})

The template I used for it is:

    <div class="well hoverwell">
    <div class="row">
        <h2 class="col-md-4">{{data.name}}</h2>
    </div>
    <div class="row">
        <span class="col-md-1">Code:</span>
        <span class="col-md-1">{{data.id}}</span>
    </div>
    <div class="row">
        <span class="col-md-1">accountability:</span>
        <span class="col-md-1">{{data.parent}}</span>
    </div>
    <div class="row">
        <span class="col-md-1">&nbsp;:</span>
        <span class="col-md-1">{{data.definition}}</span>
    </div>
</div>

And my controller code is:

'use strict';

angular.module('app')
    .controller('DataCtrl', function ($scope, DataService, $log) {

        $scope.getDataList = function () {
            var list = DataService.getDataList(1);
            list.then(
                function (result) {
                    $log.info(result);
                    $scope.dataList = result;

                }, function (status) {
                    $log.error(status)
                    $scope.msg = "error " + status + " has occurred, please report to admin ";
                });
        };

    });

When I run my app, it doesn't work. Upon checking Chrome development tools, I noticed that my directive is commented out. What could be the problem? How can I ensure that this directive is called as soon as the page loads?

Answer №1

It seems like you've noticed the empty list because your dataList in ng-repeat hasn't been populated yet.

However, there are a few errors in your code that need addressing:

  1. Firstly, using one controller twice is not recommended. It's best to create a separate controller for your directive.
  2. The replace directive parameter is deprecated and should not be used.
  3. In your DataCtrl, you set the dataList variable as $scope.dataList = result;, but in your HTML you refer to it as list:
    <li ng-repeat="data in list" class="col-md-5">
    .

Perhaps this example can help you better understand your 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

Experiencing an error while trying to implement a node.js server-side service through AngularJS. Encountering the error message: "Error: [$injector:nomod] Module ‘angularjsNode

An error has occurred: Error: [$injector:modulerr] Failed to instantiate module angularjsNodejsTutorial due to: Error: [$injector:nomod] Module ‘angularjsNodejsTutorial’ is not available! You either misspelled the module name or forgot to load it. If r ...

What is the best way to pick an option from a dropdown menu using angularjs?

Is there a way to select an item from a dropdown list in AngularJS without using ng-repeat with an object array? I can easily select an option when using ng-repeat, but how can this be achieved with a normal select list? <select class="form-control" ...

AngularJS directives and controller scope

I'm looking to create a custom directive in AngularJS that generates a div element as a title and a ul list below it. The title should be customizable through an attribute, while the list content is controlled by a designated controller. Check out t ...

Using AngularJS to Nest ng-view within ng-repeat

I have a collection of items. Each item has buttons to display its details and comments within this ng-view section. It is necessary for the user to view all available product details on one page, for example. Below is the HTML list: <div ng-controll ...

Dealing with query strings within routeprovider or exploring alternative solutions

Dealing with query strings such as (index.php?comment=hello) in routeprovider configuration in angularjs can be achieved by following the example below: Example: angular.module('app', ['ngRoute']) .config(function($routeProvider, $loc ...

What are the benefits of reverting back to an Angular 1 directive instead of using an Angular 2 application

Our team has created numerous Angular 1 components and is eager to incorporate them into our Angular 2 application. However, the documentation suggests that we must downgrade the Angular 2 application in order to integrate and utilize the Angular 1 direc ...

"Utilizing JSON data to implement custom time formatting on the y-axis with AmCharts

Looking to convert minutes to hh:mm:ss format in my JavaScript code var allDataTime = [{ date: new Date(2012, 0, 1), "col": "LONG CALL WAITING", "duration1": '720', "duration2": '57', "duration3": ...

AngularJSError

I am completely new to AngularJS and I've been tasked with debugging someone else's code. While debugging in Google Chrome, I encountered the following error: TypeError: accountService.logIn(...).success is not a function. The issue lies with ...

Here is how you can invoke a function in an AngularJS controller following a change in the $

My controller contains a method called goToNext() that is intended to change the angular.controller('myCtrl',function(){ function goToNext(){ $location.path('/abc.html'); // I also need to execute some code based on the DOM of ...

Updating the fixed value and sending it to subordinate controllers in Angular version 1.4.9

I'm facing an issue with updating the value of a constant from the root controller. When the state transitions to a login controller, the constant still holds the old value. The CONSTANT is initially defined like this: var myApp = angular.module("ap ...

Retrieve information from Angular service's HTTP response

Calling all Angular/Javascript aficionados! I need some help with a service that makes API calls to fetch data: app.service("GetDivision", ["$http", function($http){ this.division = function(divisionNumber){ $http.post("/api/division", {division:di ...

dirpagination fails to display all rows in the dataset

I've been working on creating tables with 3 divs, as shown in the link below:- https://github.com/anirbanmishra/congress.php/blob/master/web_test Additionally, I have a javascript file available here:- https://github.com/anirbanmishra/congress.php/bl ...

Visual Studio build is disrupted by the presence of node_modules folder

I am currently in the process of integrating Angular (v4) into an existing ASP.NET MVC 4 application. Within this application, there is a project that includes Selenium Web Driver along with a web.config file. node_modules\selenium-webdriver\lib ...

Displaying the structure of a MongoDB database using Express and Angular in a tabular format

I am looking to present the data from MongoDB in a table format using HTML along with Node.js, Express.js, and Angular.js. Currently, my approach is as follows: route.js app.get('/superhero', function(req, res) { superhero.superhero_list(r ...

Forwarding AngularJS events using socket.io with the use of deferred promises

I recently started using the Angular library angular-socket-io for my project. However, I encountered a challenge where I needed to implement authentication within the app before initializing the socket.io interface. To address this issue, I found and impl ...

Emulate an AngularJS ng-click action

My website has HTML code with three buttons: <button ng-click='showStats(player.data,0)'>Death Match</button> <button ng-click='showStats(player.data,1)'>Champions Rumble</button> <button ng-click='sho ...

Issues with injection of angularjs, sockjs, and angular-sockjs are causing functionality to not

As a newcomer to technologies like angular, sockjs-client, and cyclone, I've encountered an injection issue while attempting to utilize a component created by bendrucker. The component in question can be found at this link: https://github.com/bendruck ...

What is the best way to center align and add an icon to the header in Ionic?

How can I center align an "ion-ios-arrow-up" icon in the header of my Ionic app, similar to the concept shown below? This is my HTML template: <ion-view cache-view="false" view-title="Historical HPP"> <ion-nav-bar class="nav-title-slide-ios7 ...

How to format values in a textarea using AngularJS

Is there a way to address the following issue without replacing \n with other values? A user can input a description for something in a textarea and include line breaks. In the controller, there is a value called description which includes a string ...

Plupload is not compatible with ng-dialog

I'm currently attempting to integrate plupload into a modal window that is generated by ng-dialog. Here is the code I am using: $scope.showManager = function(){ ngDialog.open({ template: '/template/fmanager.html', controller: &apo ...