Utilizing an alternative ng-controller in conjunction with ng-include

When utilizing ng-include, I face an issue where I am unable to change the controller by setting ng-controller inside the include. The controller seems to not be recognized by the include. However, it does work when I define the controller in the calling div that includes it.

For example:

Having an include like this:

<div ng-controller="baseController">
  <div ng-include="other.html"></div>
</div>

The other.html is defined as follows:

<div ng-controller="subController">
  <ul>
    <li ng-repeat="address in addresses">
      {{ address.name }}
    </li>
  </ul>
</div>

In this scenario, the addresses will not be displayed. But if I alter the code to this:

 <div ng-controller="baseController" ng-controller="subController">
    <div ng-include="other.html"></div>
 </div>

The other.html is then defined as:

<div >
  <ul>
    <li ng-repeat="address in addresses">
      {{ address.name }}
    </li>
  </ul>
</div>

Everything works perfectly and the addresses are successfully displayed.

Wondering why?

Best regards,

Jaap van der Kreeft

Answer №1

For the solution, please click on this link

<div ng-controller="baseController">
  <div ng-include src="'other.html'"></div>
</div>

To include the inner quotes when passing src as a string is necessary. This approach is suitable for files that remain constant. However, if there is a possibility of file changes, it's recommended to assign the value to an Angular variable and reference it in the tag.

For example:

<div ng-include src="templatePath"></div>

Additionally, the script code to implement this is:

 $scope.templatePath = "other.html";

Answer №2

Give this a shot:

<div ng-controller="mainController">
  <div ng-include="'template.html'"></div>
</div>

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 process of passing the ID value to another function is malfunctioning in JavaScript

Currently working on a sudoku puzzle as part of a toy project. My goal is to retrieve the ID value of blank spaces and pass it to another function. However, I am facing an issue where the numbers are not getting inserted into the blanks upon clicking. Can ...

React Prop Local Modal Redux

Just diving into the world of React and Redux, and I'm a bit lost on how to effectively utilize both local properties and redux properties simultaneously. After trying different approaches without success, I'm reaching out for guidance. My goal i ...

Arranging Typescript strings in sequential date format

Looking for guidance on how to sort string dates in chronological order, any expert tips? Let's say we have an array object like: data = [ {id: "1", date: "18.08.2018"} {id: "2", date: "05.01.2014"} {id: "3", date: "01.01.2014"} {id: ...

What could be the reason for this code returning a "module not found" error?

Within localbase.js... const fs = require("fs"); if(!fs.existsSync(__dirname + "/localbase.json")) fs.writeFileSync("./localbase.json", "{}"); let database = require("./localbase.json"); The code abov ...

Update the choices in a select dropdown based on the data selected in another form's select field in real-time

Do you think it can be done? If so, how exactly? I've been looking for an example but haven't found anything yet. (I did come across a case of updating options dynamically based on another select field's data within the same form). ...

toggle the visibility of an element depending on the function's outcome

When a user logs in to my system, I want the logout button to appear. When logged out, the button should disappear. To implement this, I tried using the following code for a button click: $scope.test = function () { if ($rootScope.authenticated ...

Incorporate a course within the conditional statement

Currently, I'm working on the development of an input site and one of my goals is to highlight empty fields. My plan is to check if a field is empty using an if statement and then apply a specific class that will serve this purpose. This is the JavaS ...

Can the page's user interface stay the same even after refreshing the page?

Are you considering including a test case to verify if all checkboxes are unchecked after the page reloads? Is it possible for checked checkboxes to remain selected even after reloading or navigating away and returning to the page? Could this issue be due ...

Applying a value to all JSON objects within an array using AngularJS and JavaScript

Tale: I've developed an array ($scope.mainArray) that will be displayed in a <table> with <tr> elements using ng-repeat as shown below: +---+ | 1 | +---+ | 2 | +---+ | 3 | +---+ Each object contains an array which is presented within & ...

Issues with navigating sliders

I've encountered a problem with my slider arrows while following a YouTube tutorial. They are supposed to appear next to the picture and testimonial, but instead, they are showing up at the top. This issue wasn't present initially, but after enco ...

Loading a .css file in advance using NextJS

We have integrated NextJS and Material-UI into our website, but we are facing an issue with FOUC (Flash of Unstyled Content) upon page loading. After some investigation, I discovered that the JS files are loading faster than the CSS file, causing the probl ...

IFrame displaying blank page instead of report

The following code snippet displays a report within an IFrame: HTML <div id="dialogReport"> <div> <iframe id="reportFrame" style="width: 800px; height: 600px; border: 2px solid black; margin: 10px auto;"> </iframe> </di ...

Utilizing Vue alongside Laravel by passing null values as props

Is it permissible to provide a null prop? I have created a main component that accepts the user prop. <div id="app"> <master :user="{{ $user }}"></master> </div> The prop is declared as follows: props : { user: { ...

Express displaying undefined when referring to EJS variable

After receiving valuable assistance from user Jobsamuel, I have been encountering challenges in displaying the data retrieved from an API call on a webpage: // EJS template to show API data. app.get('/activities', function (req, res) { if (re ...

Leveraging pre-rendered HTML in Vue.js components for both parent and child elements

Currently, I am rendering all the HTML server-side and attempting to use Vue to set this HTML as the $el for two components. According to the lifecycle diagram, this should be possible. There is a parent Vue instance (which binds to #main) that contains a ...

Dynamically insert a new row into an HTML table using AJAX and refresh the table with .load method

I am facing an issue with my HTML table that loads data dynamically through a PHP snippet containing SQL queries. There is a Select option and a button on the table to add a new row, which triggers an AJAX procedure to send the data to PHP for insertion in ...

only one of the ng-bind-html elements on the page is functioning

I am currently working on an AngularJS application and encountered a problem with this block of code. Only the first ng-bind-html works for me: <div ng-bind-html='newsTitle'> <div ng-bind-html='newsDetail'></div> &l ...

A comprehensive guide on implementing filtering in AngularJS arrays

I am working with the array provided below: [ { Id: 1, Name: "AI", Capacity: 2, From: "2021-10-27T08:00:00", To: "2021-10-27T08:50:00", }, { Id: 2, Name: "TEST", Capacity: 2, ...

Determine if a JavaScript code in my ASP view will be executed based on control from the controller

Working on an ASP.NET MVC4 system, I have a javascript code that displays a performance graph of students when the page loads. Here is the code for the graph: <script> window.onload = function () { var r = Raphael("holder"), ...