1

I am completely new in Angular. I want to load two different partial views with singleton controller by using ng-Route and ng-view.But every time when I executed below SpecialFields.cshtml that I got this type of error.

I have searched many times and test a lot of points in stack overflow like this: angular multiple routes sharing one controller. But code didn't work properly.

How can i solve this problem and how can I render partial views by ng-view and ng-route? Thanks in advance

SpecialField.cshtml is:

    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/Scripts/angular-route.js"></script>
    <script src="~/App/Patients/SpecialFields/specialFieldController.js"></script>
    <script src="~/App/Common/Services/StorageService.js"></script>
    <div class="row" style="font-family:BYekan">
        <div class="col-md-12">
            <div class="portlet light portlet-fit bordered">
                <div class="portlet-title">
                    <div class="caption">
                        <i class=" fa fa-cog font-green"></i>
                        <span class="caption-subject font-green bold uppercase">SpecialFields config</span>
                    </div>

                </div>
                <div class="portlet-body" ng-app="SpecialFields" ng-controller="SpecialFieldsController">
                    <div class="row" style="margin:0 1px;direction:rtl">
                        <div class="col-md-12">
                            <div class="row panel panel-default">
                                <div class="panel-body">
                                    <form name="frm" class="form-inline" >
                                        <div class="form-group col-md-6">
                                            <label for="fieldname" class="bold control-label">Field Name</label>
                                            <span class="required">*</span>
                                            <input type="text" name="fieldname" id="fieldname" class="form-control" required ng-model="specialfieldname" />
                                        </div>
                                        <div class="form-group  col-md-5 col-md-pull-1" >
                          <label for="fieldtype" class="bold control-label">Field Type:</label>
                                            <span class="required"> * </span>
                                            <select id="fieldtype" class="form-control" ng-model="specialfieldtype" ng-options="option for option in [Item1,Item2]">
                                                <option value="" class="placeholder" selected disabled>Item1</option>
                                            </select>
                                            <button class="btn btn-primary " type="submit" ng-click="addSpecialfield()">
                                                Add
                                                <span class="fa fa-plus-square-o" style="padding-right:5px;"></span>
                                            </button>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class=" col-md-12">
                            <table class="table table-striped table-bordered" id="sfields">
                                <thead>

                                    <tr>
                                        <th> Index</th>
                                        <th>Field name</th>
                                        <th>Field Type</th>
                                        <th>Edit</th>
                                        <th>Delete</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="specialfield in specialfields">

                                        <td class="col-sm-2">
                                            {{$index+1}}
                                        </td>
                                        <td class="col-sm-4">
                                            {{ specialfield.specialfieldname }}
                                        </td>
                                        <td class="col-sm-4">
                                            {{ specialfield.specialfieldtype }}
                                        </td>
                                        <td class="col-sm-1">
                                            <a href="#/update" class="btn btn-default">
                                                Edit
                                                <span class="glyphicon glyphicon-check"></span>
                                            </a>
                                        </td>
                                        <td class="col-sm-1">
                                            <a href="#" class="btn btn-danger" ng-click="deleteSpecialfield(specialfield)">
                                                Delete
                                                <span class="fa fa-remove"></span>
                                            </a>

                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

this is specialFieldController.js

var isHtml5Compatible = document.createElement('canvas').getContext != undefined;

if (isHtml5Compatible) {
    initiateLocalStorage();

}

function initiateLocalStorage() {
    // Create the AngularJS app
    var app = angular.module('SpecialFields', ['common']);
    app.config(function ($routeProvider) {
        $routeProvider
        .when('/', {
            controller: 'SpecialFieldsController',
            templateUrl: '/List.html'
        })
        .when('/update', {
            controller: 'SpecialFieldsController',
            templateUrl: '/Update.html'
        })
    });


    // Create the Controller
    app.controller('SpecialFieldsController', ['$scope', 'getLocalStorage', function ($scope, getLocalStorage) {

        //Read the Sfield List from LocalStorage
        $scope.specialfields = getLocalStorage.getSpecialfields();


        //Count the SpecialFields List
        $scope.count = $scope.specialfields.length;


        //Add Sfield - using AngularJS push to add sfield in the sfield Object
        //Call BindTable_SpecialField to update the locally stored Sfield List
        //Reset the AngularJS SpecialField scope
        //Update the Count
        $scope.addSpecialfield = function () {
            $scope.specialfields.push({'specialfieldname': $scope.specialfieldname, 'specialfieldtype': $scope.specialfieldtype });
            getLocalStorage.BindTable_SpecialField($scope.specialfields);
            //$scope.sfno = '';
            $scope.specialfieldname = '';
            $scope.specialfieldtype = '';
            $scope.count = $scope.specialfields.length;
        };

        //Delete Sfield - Using AngularJS splice to remove the sfield row from the sfield list
        //All the Update sfield to update the locally stored sfield List
        //Update the Count
        $scope.deleteSpecialfield = function (specialfield) {
            $scope.specialfields.splice($scope.specialfields.indexOf(specialfield), 1);
            getLocalStorage.BindTable_SpecialField($scope.specialfields);
            $scope.count = $scope.specialfields.length;
        };
    }]);
}

and this is StorageService.js

var storageService = angular.module('common', []);
storageService.factory('getLocalStorage', function () {
    var specialfieldList = {};
    return {
        list: specialfieldList,
        BindTable_SpecialField: function (SpecialfieldsArr) {
            if (window.localStorage && SpecialfieldsArr) {
                //Local Storage to add Data
                localStorage.setItem("specialfields", angular.toJson(SpecialfieldsArr));
            }
            specialfieldList = SpecialfieldsArr;

        },
        getSpecialfields: function () {
            //Get data from Local Storage
            specialfieldList = angular.fromJson(localStorage.getItem("specialfields"));
            return specialfieldList ? specialfieldList : [];
        }
    };

});

** Edit 2: Div with class portlet-body will have ng-view directive and will be my placeholder of my views.all of codes in this div will move to List.html.another html file that will be rendered is Update.html which it renders when Edit button will be clicked.

Community
  • 1
  • 1
Ela
  • 11
  • 3
  • Have you checked all scripts are loading? That error can also be displayed if there are syntax errors in your code also. Using same controller for multiple routes isn't common but shouldn't cause a problem either – charlietfl Oct 03 '16 at 21:38
  • note...you never injected `'ngRoute'` as module dependency – charlietfl Oct 03 '16 at 21:42
  • Yes I checked it thousand times.I have just one controller and I mainly I want to load update.html.The default partial view is list.html – Ela Oct 03 '16 at 21:42
  • there are two different modules.one of them is about local storage and I called it common module .specialField is module that has specialFieldController – Ela Oct 03 '16 at 21:45
  • right but to use ngRoute ...it is also a module and needs to be injected – charlietfl Oct 03 '16 at 21:47
  • Yes you're right .I injected it but it had this error again – Ela Oct 03 '16 at 21:49
  • will also confuse you if you think that the outer `ng-controller="SpecialFieldsController"` will be same controller instance as what you declare in the routing...will be multiple instances – charlietfl Oct 03 '16 at 21:50
  • So what do I do exactly? – Ela Oct 03 '16 at 21:54
  • need to solve module error first. Suggest commenting out the inner code of controller and service so just the component declarations exist and see what happens – charlietfl Oct 03 '16 at 21:57
  • I inject the 'ngRoute' to module and fix the error.but there is no redirecting to update.html when I click Edit Button – Ela Oct 03 '16 at 21:57
  • are those templates in root of your site? Can inspect the request for them in browser dev tool network also. And you can add a `$routeChangeError` handler as well – charlietfl Oct 03 '16 at 22:00
  • no there are in app/patients/specialField folders – Ela Oct 03 '16 at 22:01
  • well paths shown are for site root ...look at network requests. Also don't see any ng-view – charlietfl Oct 03 '16 at 22:02
  • So how do I give this path?I have to give a relative path? – Ela Oct 03 '16 at 22:09
  • relative or absolute doesn't matter but it needs to be correct. Using a leading `/` makes it absolute starting at site root – charlietfl Oct 03 '16 at 22:11

0 Answers0