0

I am working on MVC4 with angular JS and using ng-include to call a partial view inside my main view. Issue comes when I am trying to click a button inside my partial view.

I have two different controllers in angular, one for main and other one for a partial view both are working under same module.

My file structure is 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");
    };
  });

and this is the HTML, I am using for partial page

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

The partial view is working fine when its called up independently(not inside the main view). No error is coming inside the browser but click event is not working.

1
  • why your partial html again contain an body tag? Commented Jun 5, 2015 at 6:37

2 Answers 2

1

The problem is probably because you are calling the "app" twice. Once in your layout (html) page and then in your partial page. You can fix this by replacing:

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

With:

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

NOTE: The change from body to div (can be any html container tag) and removal of ng-app directive.

Sign up to request clarification or add additional context in comments.

2 Comments

It has started working but why we need to change the parent tag from body to div ? If I am keeping the ng-controller in my body its not working.
You need to change the partial page's tag from body to div (or any other container tag e.g. section) because there's already a body tag in the layout file (if I'm not mistaken about MVC4). And you with html, you can only have 1 body tag. You also need to remove ng-app from the partial because like the body tag, you only need 1 ng-app directive
0

Here! You have same module name so it will consider first one. Just different name both and it will works..

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>

4 Comments

I want to use the very same module. And if I use your code it will give me error as dependency is not defined for new module. Please have a look here you will get better understanding of what I am looking for. stackoverflow.com/questions/20087627/…
Just add app = angular.module("Layout", ['newLayout']); and angular.module("newLayout",[]) your error go..
please check spelling mistake.. your code is proper but somewhere minor mistake of spelling in name...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.