2

Hey AngularJS wizards!

I'm trying to implement something similar to the following plnkr. The original post.

http://plnkr.co/edit/mzQhGg?p=info

var projectsApp = angular.module('projects', ['ngResource']);

projectsApp.config(function($routeProvider) {
  $routeProvider
          .when('/', {
    controller: 'ProjectListCtrl',
    templateUrl: 'projectlist.html'})
          .when('/project/:id', {
    controller: 'ProjectDetailCtrl',
    templateUrl: 'projectdetail.html'
  })
          .otherwise('/');
});

projectsApp.factory('Project', function($http) {
  var json = $http.get('project.json').then(function(response) {
    return response.data;
  });

  var Project = function(data) {
    if (data) angular.copy(data, this);
  };

  Project.query = function() {
    return json.then(function(data) {
      return data.map(function(project) {
        return new Project(project);
      });
    })
  };

  Project.get = function(id) {
    return json.then(function(data) {
      var result = null;
      angular.forEach(data, function(project) {
        if (project.id == id) result = new Project(project);
      });
      return result;
    })
  };

  return Project;
});

projectsApp.controller('ProjectListCtrl', function(Project, $scope) {
  $scope.projects = Project.query();
});

projectsApp.controller('ProjectDetailCtrl', function(Project, $routeParams, $scope) {
  $scope.project = $routeParams.id
          ? Project.get($routeParams.id)
          : new Project();
});

In this example, it works perfectly but I notice that the angularjs references is 1.0.7 and not the latest 1.2.26. Once I update the script reference to 1.2.26, the example breaks. I believe the issue is on line 21 in the app.js file where angular.copy(data, this) is called.

I'm still a newbie to angularjs. I tried googling and failed to figure out what the breaking changes are between 1.0.7 and 1.2.26. I found this and searched for angular.copy(), but I just don't understand the ramifications related to my example. Plus I want to understand it better.

Thanks in advance!

Community
  • 1
  • 1
  • I think your issue isn't the copy but the use of `this`. http://stackoverflow.com/questions/11605917/this-vs-scope-in-angularjs-controllers – Matthew Green Oct 22 '14 at 15:56

1 Answers1

0

You have to include angular routing js in your code. Because in latest version, routing separated from general angular js script.

Thanks, Sandeep Garg