1

Previously, my code works fine in AngularJs 1.1.5 version, but after upgrade to 1.2.x, the following codes don't work. Are there anything changed in 1.2.x?

Here is the demo: http://plnkr.co/edit/w2O8Ci It cannot show the "world", but if you change the angularjs back to 1.1.5, everything's working fine.

Here is the code:

<!DOCTYPE html>
<html ng-app="myApp">

<head>
    <!--<script src="http://code.angularjs.org/1.1.5/angular.min.js"></script>-->
    <script src="http://code.angularjs.org/1.2.9/angular.min.js"></script>
    <script>
        var app = angular.module('myApp', []);
        app.factory('NameFactory', function($http, $q) {
            return {
                getName: function() {
                    var deferred = $q.defer();
                    deferred.resolve({
                        name: "World"
                    });
                    return deferred.promise;
                }
            }
        });

        app.controller("NameCtrl", function($scope, NameFactory) {
            $scope.name = NameFactory.getName();
        });
    </script>
</head>

<body ng-controller="NameCtrl">
<h1>Hello, {{name.name}}</h1>
</body>

</html>
PunCha
  • 258
  • 2
  • 11
  • 1
    possible duplicate of [Angularjs promise not binding to template in 1.2](http://stackoverflow.com/questions/19472017/angularjs-promise-not-binding-to-template-in-1-2) – flytzen Jan 17 '14 at 19:14

2 Answers2

3

Yes, something did change in 1.2 and views can no longer automatically unwrap promises. See Angularjs promise not binding to template in 1.2

Community
  • 1
  • 1
flytzen
  • 7,348
  • 5
  • 38
  • 54
2

You're using promises in the way that $resource works...not the way promises work. You should do this in your controller The promise API changed in 1.2

  NameFactory.getName().then(function(data){
    $scope.name = data
  });
calebboyd
  • 5,744
  • 2
  • 22
  • 32