1

The following code does not display firstName and lastName on screen. can any one point out what am I missing? Thanks.

<body ng-app="myApp">
<div ng-controller="myCtrl">
 Full Name: {{name.firstName + " " + name.lastName}}
</div>
<script src="../bower_components/angular/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function() {
 var name={
 firstName: "John",
 lastName: "Doe"
 }
 this.name=name;
});
asked Nov 26, 2015 at 4:29
1
  • Use $scope . In this case, use it as $scope.name in your controller Commented Nov 26, 2015 at 4:34

1 Answer 1

4

I think you are trying controller as syntax.If so

<div ng-controller="myCtrl as myctrl">
 Full Name: {{myctrl.name.firstName + " " + myctrl.name.lastName}}
</div>

Otherwise you have to inject $scope and set name variable in $scope object.

var app = angular.module('myApp', []);
app.controller('myCtrl', ['$scope',function($scope) {
 var name={
 firstName: "John",
 lastName: "Doe"
 }
 $scope.name=name;
}]);
answered Nov 26, 2015 at 4:34
Sign up to request clarification or add additional context in comments.

1 Comment

please reedit your ans $scope.name=name; }]); that square bracket comes before round bracket.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.