Struggling to retrieve scope variable within ng-template inside script tag

Trying to create a template for inline editing with Angular, I found this useful tutorial:

Here is my code:

<table class="table">
        <tbody>
            <tr class="table-row" ng-repeat="admin in admins" ng-include="getTemplate(admin)"> 
                <script type="text/ng-template" id="display">
                    <td class="table-img">
                       <img src="images/in.jpg" alt="" />
                    </td>
                    <td class="table-text">
                        <h6>{{admin.firstname}} {{admin.lastname}}</h6>
                        <p>{{admin.email}}</p>
                        <p>{{admin.phone}}</p>
                    </td>
                    <td>
                        <span class="fam">{{admin.dept_name}}</span>
                    </td>
                    <td class="march">
                        <button class="btn-transparent btn" ng-click="editAdmin(admin)"><i class="fa fa-edit fa-2x"></i></button>
                    </td>
                    <td>
                        <button class="btn-transparent btn"><i class="fa fa-remove fa-2x"></i></button>
                    </td>
                </script>
                <script type="text/ng-template" id="edit">

                </script>
            </tr>
        </tbody>
    </table>

AngularJS code:

$scope.getTemplate = function (admin) {
        if (admin.id === $scope.selected.id){
            $scope.template = 'edit';
        }
        else{
            $scope.template = 'display';
        }
        return $scope.template;
    };
    $scope.editAdmin = function (admin) {
        $scope.selected = angular.copy(admin);
    };

However, when running the code, it throws a 404 error in the console:

www.ip-address/dashboard/principal/display

The script ID "display" seems to be causing this issue.

Why is it trying to access the "display" ID in the URL?

The admins scope variable contains data, but it isn't accessed when written under the script.

Any help would be appreciated.

Answer №1

When you encounter a 404 error, it means that the requested information was not found.

 $scope.getTemplate = function (employee) {
        if (employee.empID === $scope.selected.empID){
            return 'edit';
        }
        else return 'display';
    };

The getTemplate function checks if the ID already exists and determines whether to open the employee record in Edit mode or Display mode.

HTML

<tbody>
                    <tr ng-repeat="employee in employees" ng-include="getTemplate(employee)">
                    <script type="text/ng-template" id="display">
                        <td>{{employee.empName}}</td>
                        <td>{{employee.empEmail}}</td>
                        <td>{{employee.empSalary | currency:"&#8377;"}}</td>
                        <td>{{employee.active | active}}</td>
                        <td>
                            <button type="button" class="btn btn-primary" ng-click="editEmployee(employee)">Edit</button>
                            <button type="button" class="btn btn-danger" ng-click="deleteEmployee(employee)">Delete</button>
                        </td>
                    </script>
                    <script type="text/ng-template" id="edit">
                        <td><input type="text" ng-model=employee.empName class="form-control input-sm"/></td>
                        <td><input type="text" ng-model=employee.empEmail class="form-control input-sm"/></td>
                        <td><input type="text" ng-model=employee.empSalary class="form-control input-sm"/></td>
                        <td>
                            <select class="form-control input-sm" ng-model=employee.active>
                                <option value='1'>Yes</option>
                                <option value='0'>No</option>
                            </select>
                        </td>
                        <td>
                            <button type="button" class="btn btn-primary" ng-click="updateEmployee(employee)">Save</button>
                            <button type="button" class="btn btn-danger" ng-click="reset()">Cancel</button>
                        </td>
                    </script>
                    </tr>
                </tbody>

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

Function in Node.js/JavaScript that generates a new path by taking into account the original filepath, basepath, and desired destination path

Is there a custom function in Node.js that takes three arguments - filePath, basePath, and destPath - and returns a new path? For example: Function Signature Example var path = require('path'); // Could the `path` module in Node be useful here? ...

Exploring the presence of duplicates in JavaScript and/or AngularJS

While working on my AngularJS app, I found myself iterating through a .csv file to perform element level validation. Since I couldn't find a suitable library in Angular, I decided to write custom JavaScript code to handle this scenario. Below is a sam ...

Discovering the meeting point where two dives converge

Is there a method to change the color of the overlapped common part of two animated circles? ...

There was an error with the response code of 500 on the development server for react-native

An issue has been encountered on the node.js window: error: bundling failed: Error: Unable to resolve module react-native-gesture-handler from node_modules\@react-navigation\native\src\Scrollables.js: react-native-gesture-handler could ...

Restrict the number of labels on Chart.js exclusively for smaller displays

How to Limit the Number of Labels on a Chart.js Line Chart I am looking for a way to decrease the number of labels on my line chart only when it is viewed on a small device. The solution provided in the previous post suggests using the following code: xAx ...

Tips for emphasizing specific text within HTML tags without highlighting the tags in Vue

I am using a tag with v-html to render HTML text and display it, like so: <div v-html="htmlText"></div> I have written code to highlight text and it works on regular text: Vue.filter('highlight', function (word, query) { ...

When running the command "npx create-next-app@latest --ts," be aware that there are 3 high severity vulnerabilities present. One of the vulnerabilities is that "node-fetch

Just set up a fresh project with Next.js and TypeScript by following the documentation using npx create-next-app@latest --ts. Encountering multiple high severity vulnerabilities despite running npm audit fix --force, which actually adds more vulnerabiliti ...

Looking to prevent printing and saving a webpage directly from the file menu using JQUERY or JavaScript

I am in need of assistance to find a code that can disable the ability to print and save a webpage using JQUERY or JAVASCRIPT ...

A guide on implementing a .click function with jQuery

I have a code snippet that is supposed to add 100 to a number in a MySQL table when a button is clicked. However, the code does not work as expected. When I click the button, nothing happens, but if I refresh the page, it adds 100 to the number. Can some ...

Issue with HighCharts: Bar columns not extending to the x-Axis when drilling up

I am encountering an issue with HighChart/HighStock that I need help with. To illustrate my problem, I have set up a JSFiddle. The problem arises when a user drills down on a bar column, causing the y-axis to shrink and consequently making the x-axis appea ...

Adjusting element placement on collapsed navbar using Bootstrap

I've been developing a navbar for a webpage and have successfully aligned the data left and right with the Brand in the center. However, when the navbar collapses, the alignment shifts to Left (over) Brand (over) Right. I want the Brand to remain at t ...

Selecting JavaScript Libraries During the Development of a Web Application

Transitioning from PHP & Java/Android development to web app development can feel overwhelming, especially with the abundance of Javascript Frameworks available. Check out this comparison of popular JavaScript frameworks View a list of the top JavaSc ...

What causes the Vuetify checkbox to trigger twice when clicked in a Vue.js application?

I am facing an issue with a table that contains checkboxes in every row. I want to trigger some logic when a checkbox is clicked. In some cases, I need to tick multiple rows when a checkbox is clicked, so I have set the checkboxes as readonly and handle th ...

My month sorting function is designed to work efficiently, although it seems to only be effective on the first

I am currently using a combination of JavaScript and Angular to sort through my data. Initially, I attempted to filter the data by month and it worked as expected the first time. However, for some reason, it stopped functioning properly afterwards, causing ...

I am looking to modify the JSON format of the API response

Received this response from the API and need to convert the JSON format below: Original: "data": [ [ { "isFile": "true", "fileType": "image", "filesize": 100793, ...

How to Automatically Display the First Tab in Bootstrap 3

Having a dynamic modal with two tabs, I aim for #tab1 to always be the default tab upon opening the modal. The issue arises when the modal is opened, #tab2 is clicked, and then the modal is closed. Subsequent reopenings still display #tab2. See JSFiddle e ...

The Bitcoin API response is coming back with a null body

I have been diving into the world of APIs and am currently working on a web application that converts cryptocurrencies to fiat currencies. However, I am facing an issue while trying to parse the JSON from the API call. Every time I attempt to do so, I enco ...

Unable to execute Protractor using Node.js command line

Hi there! Currently, I am in the process of setting up protractor for the very first time using Node.js. I found detailed instructions on how to do this on the AngularJS website under the section "Running E2E Tests": https://docs.angularjs.org/tutorial Ho ...

Using "this" in callback functions in JS references the current context

Part 1: Is there a way to reference a parent property dynamically from within a callback? Consider the following scenario: var someClass = { htmlClass : 'aFunClass', bindEvents: function(){ jQuery(function(){ alert( th ...

Twice within a controller, alert is displayed in AngulasJS, OnsenUI, and Phonegap

I'm currently developing a mobile application using AngularJS, OnsenUI, and PhoneGap to package it for Android devices. However, I've encountered a strange issue where an alert box pops up twice when the application is run as a new process. I&ap ...