javascript - $routeParams undefined when passing to new view & controller -
fairly new angularjs , webapi here, , figure best way learn doing. apologies in advance if question seems simple - i've spent day flipping through stackoverflow , tried them all.
i have separate master & detail view, both own controller. trying pass selected id through details controller can query database using id, though getting "undefined" on $routeparams. i'm unsure if missing simple, or whether i'm approaching correctly.
the controller doesn't seem when inject '$routeparams' either.
my app.js module:
var app = angular.module("projectdashboardmodule", ["ngroute"]); app.config(['$routeprovider', '$locationprovider', function ($routeprovider, $locationprovider) { $routeprovider .when("/", { templateurl: "/home/index" }) .when("/project", { templateurl: '/project/index', controller: 'projectcrudcontroller' }) .when("/project/project/:id", {templateurl:'/project/project', controller: 'projecttaskcontroller' }); $routeprovider.otherwise({redirectto: '/home' }); $locationprovider.html5mode(true); }]);
my factory.js:
app.factory('projectfactory', ['$http', function ($http) { var urlbase = '/api/projects/'; var projectfactory = {}; projectfactory.getprojects = function () { return $http.get(urlbase); }; projectfactory.getsingleproject = function (id) { return $http.get(urlbase + '/' + id); }; return projectfactory; }]);
my projecttaskcontroller.js:
app.controller('projecttaskcontroller', ['$scope', "$routeparams", 'projectfactory', function ($scope, $routeparams, projectfactory) { alert($routeparams.id) $scope.project; $scope.message; getprojectbyid($routeparams.id); function getprojectbyid(id) { projectfactory.getsingleproject(id) .success(function (data) { $scope.project = data; }) .error(function (error) { $scope.message = 'error retrieving project ' + error.message; }); } }]);
i found problem angular script references scattered. moved custom script references (controller, factory, module) index.cshtml , fixed issue.
Comments
Post a Comment