Two controller files causing conflict with ng-click functionality in partial view

Currently, I am developing a project using MVC4 with angular JS and implementing ng-include to incorporate a partial view within my main view. I have encountered an issue when attempting to click a button located in the partial view.

In my setup, there are two separate controllers in angular - one for the main view and another for the partial view, both operating within the same module.

The structure of my files is organized as follows:

Scripts..

| --Main.js

|--ProjectPage.js

(Main.js)

var app = angular.module("Layout", []);
app.controller("LoadPage", function ($scope) {
    $scope.templateUrl = '/Home/DefaultPage';
};

(ProjectPage.js)

angular.module("Layout")
    .controller("CNTRL", function ($scope) {
    $scope.clickBtn1 = function () {
        alert("ABU");
    };
  });

Here is the HTML code I am utilizing for the partial page:

<body ng-app="Layout" ng-controller="CNTRL">
<button ng-click="clickBtn1()" id="click">click</button>

When the partial view is accessed independently (not nested within the main view), it functions correctly without any errors appearing in the browser. However, the click event does not trigger as expected.

Answer №1

It seems like the issue is arising because of double calling of the "app". One in your main layout (html) page and then again in your partial page. To resolve this, you can make the following adjustment:

<body ng-app="newLayout" ng-controller="CNTRL">

Replace the above code with:

<div ng-controller="CNTRL">
  <!-- button code here -->

IMPORTANT: Make sure to change from using body to div (or any other html container tag) and remove the ng-app directive.

Answer №2

Hey there! If you have the same module name, it will only consider the first one. To make this work, simply give them different names.

Main.js

var app = angular.module("Layout", []);
app.controller("LoadPage", function ($scope) {
    $scope.templateUrl = '/Home/DefaultPage';
};

ProjectPage.js

angular.module("newLayout")
    .controller("CNTRL", function ($scope) {
    $scope.clickBtn1 = function () {
        alert("ABU");
    };
});

Body content

<body ng-app="newLayout" ng-controller="CNTRL">
<button ng-click="clickBtn1 ()" id="click">click</button>

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

There was an issue with the SidebarController function being undefined, as it was expected to be a function

I'm encountering two issues with my demo: Error: [ng:areq] Argument 'SidebarController' is not a function, received undefined http://errors.angularjs.org/1.2.26/ng/areq?p0=SidebarController&p1=not%20aNaNunction%2C%20got%20undefined ...

In JavaScript, the function yields a proxy rather than an object

Let's say I have an array: const arr = ['one', 'two', 'three'] Now, imagine I have a function that is designed to take an array of strings and return an array of objects: const func = (arr) => arr.map(item => ({str ...

Is it possible to import npm modules conditionally?

Here is the structure of my project: - workspace - customPackage - customIndex.js - myProject - index.js - myProject2 - index.js During development, I need to import the package from my local workspace like this: //index.js import some ...

AngularJS is a framework that allows for the implementation of vertical synchronous

After successfully implementing horizontal synchronous scrolling, I am now trying to achieve vertical scrolling. If anyone can assist, that would be greatly appreciated. You can find the sample where horizontal synchronous scrolling works here. e.target.s ...

Adding a promise to an array using Javascript

I am facing an issue while attempting to create an array of promises and then calling them using Promise.all. The problem lies in correctly pushing the functions into the array. It seems like they are getting executed instead of being inserted and waiting ...

Steps for updating the state when a button is clicked

Recently, I've been diving into learning React but have encountered some challenges with forms. While I can successfully change a state after clicking a button in a form, my struggle lies in finding resources that guide me on how to render the new st ...

Is there a way to navigate to an external website by clicking a button in Next.js?

<button type="button" className = {styles.googleButton}> <img className = {styles.image} src = "assets/pictures/google.jpg"></img> <p className = {styles.buttonText}> Go To Google</p> <img ...

Retrieve the values of every ng-repeat input element

I need assistance with a block of code that includes 5 input text fields generated by ng-repeat. I am having trouble retrieving the values from each individual input element. Could someone please guide me on how to achieve this? Below is the HTML: <fo ...

How can I most effectively repurpose Javascript routines within VS2008?

Forgive me for asking, but is the method still utilizing "Include files"? ...

When the document loads, remember to implement a click event on the list item

With the following list: <ul id="test" > <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul> I am looking to trig ...

Argument contains an invalid left-hand side - Reference error

Currently, I have a straightforward JavaScript function in which I iterate over a series. After that, I add a value to an array and then assign it to an object. This process is part of my work on creating a c3 combination chart. However, I encountered an ...

Tips for transferring objects between AngularJS directives

Is it possible to access an object named foo that is defined/instantiated inside the link function of directive A from the link function of another separate directive? ...

Implement CKEDITOR in Laravel's dynamic text fields: Encounter error message: editor-element-conflict

I need assistance with integrating ckeditor into a dynamic field in Laravel. Below is the script for ckeditor: <script> $(function() { $('.ckeditor').each(function(){ CKEDITOR.replace($(this).attr('id'), { extraPlugins: &apo ...

Tips for integrating AsyncGenerators with Kotlin/JS

I am currently exploring the use of IPFS with Kotlin/JS, but my issue is not limited to that specific context. The functions ipfs.cat() and ipfs.get() return an AsyncGenerator, and I am uncertain about how to properly iterate over it using Kotlin (I am als ...

The object returns true when the specified condition matches the key within the object

I need assistance with a specific object query. I am looking to execute the filter function in order to retrieve a list of keys from an object where the condition is true, as shown below: myObject = { key1: { name:"key1", select:true }, ...

Maintaining Changes in Javascript and CSS

I have created a header that can slide in and out of view with a toggle function. However, I want the header to be in the down position by default, without requiring users to click the toggle every time a new page loads. I have limited knowledge of JavaScr ...

Yep, the condition within a nested object is true

Having an issue with conditional validation using yup. My goal is to make certain properties required for shipping when the checkbox is not toggled. I am utilizing yup's when feature, but I'm struggling to access the boolean value of sameShipping ...

Send multiple values as arguments to a jQuery function

Beginner question ahead: I'm using the jquery function loadNewPicture() to upload pictures and the progress() function to track the percentage of the upload. Everything is functioning correctly. My query relates to the variables that can be passed t ...

Updating a component when a prop changes

Embarking on my journey into the world of React, I find myself in need of assistance. The issue at hand involves re-rendering data within my Table component. Whenever a new API call is made to create an entry in my database using Prisma, the newly added d ...

Retrieve the value of an unmatched ng-pattern field

<div> <input type="text" name="rate" ng-model="rate" ng-pattern="/^[0-9]+(\.[0-9]{0,2})?$/"/> </div> <span class="text-warning control-label"> {{rate}} ...