0
-----IN APP CONFIG-----
$routeProvider.
        when("/", {redirectTo: "/dashboard"}).
        when("/dashboard", {template: "<div ng-include='include'>", controller="xCtrl" }).
        .otherwise({redirectTo: "/404"})

-----IN xCtrl-----
app.controller("xCtrl",function($rootScope,$scope,$filter) {
     $scope.include = "pages/abc.html";
     var original;
    $scope.filters = {p_key: ""},
    $scope.showInfoOnSubmit = !1, original = angular.copy($scope.filters),
    $scope.revert = function () {
         return $scope.filters = angular.copy(original),$scope.projectListFilters.$setPristine()
    }, $scope.canRevert = function () {
        return!angular.equals($scope.filters, original) || !$scope.projectListFilters.$pristine
    }, $scope.canSubmit = function () {
        return $scope.projectListFilters.$valid && !angular.equals($scope.filters, original)
    }, $scope.submitForm = function () {
        return $scope.showInfoOnSubmit = !0, $scope.revert()
    }
})

---- IN pages/abc.html ----

    <form name="projectListFilters" data-ng-submit="submitForm()" novalidate>
    <input type="text" data-ng-model="filters.p_key" />
     <button type="submit"
            class="btn btn-primary btn-sm"
            data-ng-disabled="!canSubmit()">Sumbit
    </button>
    <button id="reset" type="reset" class="btn btn-sm btn-warning"
            data-ng-disabled="!canRevert()"
            data-ng-click="revert()">Revert Changes
    </button>
    </form>

There are JavaScript errors in this code regarding $valid , $pristine of undefined object. But if we specify the controller in pages/abc.html file Ex:
<form name="projectListFilters" data-ng-submit="submitForm()" novalidate data-ng-controller="xCtrl">
works fine. What is the error in here ?

1
  • 1
    The ng-include create a nested child scope and the projectListFilters form would be on it instead of the controller's scope. Commented Aug 14, 2014 at 17:19

1 Answer 1

1

Use templateUrl, not template.

{
    templateUrl: "pages/abc.html",
    controller: "xCtrl",
}

This will have pages/abc.html use xCtrl as its controller. The way you are doing it now, only the <div> with the include has the controller, but the included page does not have it.

From ngInclude:

This directive creates new scope.

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

3 Comments

I need to use include because I need to get route parameters and load a template based on that parameters :(
@KalhanoToressPamuditha create a route for each template instead. In "/dashboard" you can link to the other routes that use those templates instead of doing it the ngInclude way. This should also give you more flexibility for those individual templates.
can u please describe the process a bit :) thank you :)

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.