简体   繁体   中英

Can I call Route Provider service in Controller in angular js asp.net mvc

I am using controller in my Angularjs which gets question one by one from server and i want on specific condition this controller should call a routeprovider that should change my current view with templateUrl and put into a directive

my question is can i call route provider in controller rather than module

here is my CreateController

var CreateController = ['$scope', '$http', function ($scope, $http) {
$scope.model = {

    };
................>

.................>
    $scope.NextQuestion = function () {

        $http.post('/Testing/NextQuestion', {

            ........................>

        }).success(function (newdata) {
            $scope.model = newdata;
            var inccomplevel = $scope.model.servercomplevel;
            if (qId == 10) {
                  here i need the code for routeProvider and directive please provide me help
            }
......................>
    }];

i did the route provider in app.js file there it works

here is the code for app.js when i do like this it works and when i shift the code of route provider

to the create controller in condition if qId == 10 there it does not work

var app = angular.module('app', ['ngRoute']);
app.controller('CreateController', CreateController);

    app.config(function ($routeProvider) {
        $routeProvider
        .when('/',
        {
            templateUrl: 'Partials/TestCompleted.html',
            controller: 'AppCtrl'
        })
        .otherwise({ redirectTo: '/' });
    });
    app.controller("AppCtrl",function ($scope) {
        $scope.newmodel = {

        }
    });

Instead of trying to change the value of the templateUrl on the route, you should define another route. Then you can simply switch routes.

To change the route/view, you need update the path. You can do this using the $location service .

See AngularJS : How do I switch views from a controller function?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM