javascripthtmlangularjsng-controllerng-app

How to use same ng-app for 2 different html page?


I have 2 html page and one app.js

my search-movie.html code as the following :

<!DOCTYPE html>
<html ng-app="adphorusWork">
<head lang="en">
    <meta charset="UTF-8">
    <title>Search Movie</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <script src="scripts/jquery-1.9.1.min.js"></script>
    <script src="app/app.js"></script>
</head>
<body ng-controller="searchMovieController">
    <div class="navbar navbar-default navbar-fixed-top" style="margin-top: 20px;">
        <div class="container-fluid">
            <ul>
                <li>
                    <a href="http://www.omdbapi.com/">OMDb Api</a>
                </li>
            </ul>
        </div>
    </div>
    <div class="container" style="margin-top: 50px;">
        <div class="bs-docs-section" id="">
            <div class="row">
                <div class="col-lg-12">
                    <div class="page-header">
                        <h2><a href="https://github.com/Cakmakk">My Github</a></h2>
                    </div>
                    <div class="bs-component">
                        <form class="well form-search" id="search-movie" onsubmit="return false;">
                            <fieldset>
                                <legend>Search Movie</legend>
                            </fieldset>
                            <div>
                                <label class="control-label" for="movie-name">Movie Name : </label>
                                <input type="text" id="movie-name" class="input-small" style="margin-left: 10px;">
                                <button id="btn-search-movie" type="button" class="btn-sm btn-primary" style="margin-left: 10px;" ng-click="searchMovie()">
                                    <span class="glyphicon glyphicon-search"> Search</span>
                                </button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

and my result-movie.html code as the following :

<!DOCTYPE html>
<html>
<head ng-app="adphorusWork">
    <meta charset="UTF-8">
    <title>Search Movie</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
    <script src="scripts/angular.min.js"></script>
    <script src="scripts/angular-route.min.js"></script>
    <script src="app/app.js"></script>
    <script src="scripts/jquery-1.9.1.min.js"></script>
</head>
<body ng-controller="resultMovieController">
    <div class="navbar navbar-default navbar-fixed-top" style="margin-top: 20px;">
        <div class="container-fluid">
            <ul>
                <li>
                    <a href="http://www.omdbapi.com/">OMDb Api</a>
                </li>
            </ul>
        </div>
    </div>
    <div class="container" style="margin-top: 50px;">
        <div class="bs-docs-section" id="">
            <div class="row">
                <div class="col-lg-12">
                    <div class="page-header">
                        <h2><a href="https://github.com/Cakmakk">My Github</a></h2>
                    </div>
                    <div class="bs-component">
                        <fieldset>
                            <legend>Result for Search Movie</legend>
                        </fieldset>
                        <div>
                            <button id="btn-result-movie" type="button" class="btn-sm btn-primary" style="margin-top: 40px;" ng-click="backtohome()">
                                <span class="glyphicon glyphicon-circle-arrow-left"> Home</span>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

and my app.js as the following :

(function (angular) {
    'use strict';

    var app = angular.module('adphorusWork', []);

    app.controller('searchMovieController', function ($scope) {
        $scope.searchMovie = function () {
            alert("come")
        }
    });
    app.controller('resultMovieController', function ($scope) {
        $scope.backtohome = function () {
            alert("home")
        }
    });
})(angular);

My search button is working (in search-movie.html) but my 'Home' button not working(in result-movie.html)

I'm using the same ng-app in two html page. I want to work on other button(so Home button)

Where am I doing wrong ? or How can I fix this error ?


Solution

  • you need to use Angular Ui router for this. And @Lex is correct you are not following the angular SPA framewrok.

    This is a good read :https://github.com/angular-ui/ui-router

    this link can provide a brief detail about angular ui router https://scotch.io/tutorials/angular-routing-using-ui-router