0

The fiddle looks like this:

var app = angular.module('testApp', []);

app.controller = angular.('testAppCtrl', function ($scope) {
    $scope.vehicle = {
        type: 'car',
        color: 'red'
    };
    $scope.vehicleDetail() = function () {
        return "Vehicle type : " + vehicle.type + ", "
        vehicle.color;
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="testApp" ng-controller="testAppCtrl">

<h1>
AngularJS object ng-init powered by controller example
</h1>

Type: <input ng-model='vehicle.type' />
<br/>

Color: <input ng-model='vehicle.color'/>

Vehicle details : {{vehicleDetail()}}

</div>

I was trying to initialize objects from inside controller using function, instead of using ng-init.

Problem

Why are the values not getting printed?

1 Answer 1

2

your code should correct like below,

this is how you need to define the controller, not like app.controller = angular.('testAppCtrl', function ($scope) {.

app.controller('testAppCtrl',function($scope)...

this is how you need to return the value in function vehicleDetail, In your return case you can't access vehicle properties like vehicle.color, you need to access it like $scope.vehicle.color since vehicle is in scope. and this $scope.vehicleDetail() = function () {.. is wrong it should be corrected as $scope.vehicleDetail = function () {..

$scope.vehicleDetail = function()
{
    return "Vehicle type : " + $scope.vehicle.type + ", "+ $scope.vehicle.color;
};

here is the DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

:) cheerz glad to help you

Your Answer

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.