2

I am creating application using AngularJS and I am beginner at it. I have created one common page for searching with its separate controller and its working fine, but now I want to include this page in my other pages as user control.

My Searching page is like

<div class="input-group ex col-md-3" ng-app="searchApp" ng-controller="MainSearchController">
.......
.......//My code here
</div>

My controller for Search is:

var sApp=angular.module("searchApp",[]); 
  sApp.controller("MainSearchController", function($scope){
    $scope.Merchants={};    
    var DEMerchantInfo = JSON.parse(localStorage.getItem("DEMerchantInfo"));    
        if(DEMerchantInfo == null || DEMerchantInfo == undefined){
            DEMerchantInfo = new Array;
        }
    $scope.Merchants=DEMerchantInfo;

    $scope.Search=function(){

    var DEMerchantInfo = JSON.parse(localStorage.getItem("DEMerchantInfo"));    
        if(DEMerchantInfo == null || DEMerchantInfo == undefined){
            DEMerchantInfo = new Array;
        }
            $scope.Merchants=DEMerchantInfo;
    }   
}); 

I am using search page in my other pages like:

 <body class="sidebar-mini" ng-app="MerchantApp">
  <div class="search-panels col-lg-4 col-md-4 col-xs-10">
    <div ng-include src=" 'search.html' "></div>
  </div>
 </body>

but it is not working for me. its giving error.

Error: [ng:areq] http://errors.angularjs.org/1.5.7/ng/areq?    p0=MainSearchController&p1=not%20a%20function%2C%20got%20undefined O/<@file:///.....

So can any one please suggest how I can achieve this thing.

1 Answer 1

1

This is how search page template should look like:

<div class="input-group ex col-md-3" ng-controller="MainSearchController">
    <!-- My code here -->
</div>

Note, that I removed ng-app="searchApp" from partial. In your app you will need to add ng-app="searchApp" to some parent container, like body or html.

Then you will need to configure MerchantApp like this:

angular.module('MerchantApp', [
  // some other module dependencies
  'searchApp'
])
Sign up to request clarification or add additional context in comments.

Comments

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.