Dipen Dedania
Dipen Dedania

Reputation: 1450

Template with its own controller for angular js

I am kind of new to angular JS and this might be the simple question but i am stuck on this. What i am trying to do is, I have one page and on that page I want to add a template. My template is kind of dynamic and have it's own controller. My code is something like this..

-- Main Page HTML --

<html ng-app="myApp">
    <body ng-controller="MyAppCtrl">
        <my-component>

        </my-component>
    </body>
</html>

-- Main page JS --

var myAppModule = angular.module('myApp');

myAppModule.controller('MyAppCtrl', function ($scope) {
})
.directive('myComponent', function () {
  return {
      restrict: 'E',
      templateUrl: 'myComponent.aspx'
  };
});

-- Template(myComponent.aspx) --

<html>
    <!-- include template script -->
    <body ng-controller="ComponentCtrl">
        <div ng-click="showAlert()">
            myBtn
        </div>
    </body>
</html>

-- Template page JS --

var templareModule = angular.module('myApp', []);

templareModule.controller('ComponentCtrl', function ($scope, $http) {
    $scope.showAlert = function () {
        alert("clicked");
    }
});

So for this I am unable to get alert on click.

Thanks in advance.

Upvotes: 0

Views: 1213

Answers (1)

jsparks
jsparks

Reputation: 1030

You really don't need to create a custom directive to be able to do what you describe in your question and since you're a beginner I would suggest you start simple and do without the directive. You will need to use the ng-view tag and routing. There are some other minor issues with your code, some of which were already pointed out by others in the comments. The following code worked for me.

index.html:

<html ng-app="myApp">
    <body>
        <h1>Test</h1>
        <div ng-view></div>

        <script> (Insert paths to required Angular scripts and your JS files)</script>
    </body>
</html>

App JS file:

Note: You will need to download the ngRoute JavaScript file from Angular's website and include it in your project to get the routing to work correctly. (Go to Downloads on their website, pick your version of Angular, then download ngRoute)

angular.module('myApp', [ 'ngRoute' ])
    .config(['$routeProvider',
              function($routeProvider) {
                    $routeProvider.
                      when('/', {
                              templateUrl: 'myComponent.aspx',
                              controller: 'ComponentCtrl'
                      })
    }])
    .controller('ComponentCtrl', function ($scope) {
        $scope.showAlert = function () {
            alert("clicked");
        }
    });

myComponent.aspx:

<div class="ComponentCtrl">
    <div ng-click="showAlert()">
            myBtn
    </div>
</div>

Let me know if you have questions or are still unable to get it to work. I would also suggest you check out some tutorial videos on Egghead.io's website. It helped me figure out the basics of Angular.

Upvotes: 1

Related Questions