0

I am newbie to angularjs. Still understanding the details and how it works. i am in a situation where i cannot find this simple solution. Hope someone could help me here.

I have created a folder structure

Select
->SelectModule.js
->SelectDirective.js
->SelectController.js
->SelectTemplate.html

Here are the contents inside each file.

SelectModule.js

angular.module('Select', []);

SelectDirective.js

'use strict';
angular.module('Select').directive('Select', Select);
function Select() {
return {
    transclude: false,
    scope: {

    },
    controller: 'SelectController',
    templateUrl: 'app/directives/Select/SelectTemplate.html'
};
};

SelectController.js

use strict'
var SelectMod = angular.module('Select', []);
SelectMod.controller('SelectController', function ($scope) {

console.log($scope.details.data);
});

SelectTemplate.html

<section ng-controller="SelectController">
<div><label>Data:</label><div><input type="number" ng-model= "details.data" ng-init="details.data=0.2"  step="0.1" /></div></div>
</section>

Now, in the template i have initialized details.data=0.2. If you see in the controller, i am trying the get this initialized data in templete and outputting it in the console by this statement

console.log($scope.details.data);

The problem is when i run the code and check the console. it gives error stating that "Cannot Read property 'data' of undefined". How do i get this value into the controller. if i get this thing, i will come to know how to get data from template and process in the controller. i am stuck here. someone please suggest or let me know where i went wrong.

1 Answer 1

1

The reason is the controller has to run before it can render the view, not the other way around.

So at the time you try to log the data it doesn't exist

Don't use ng-init for this purpose. The documentation for ng-init also advises not to use it this way.

Initialize variables in the controller.

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

4 Comments

@charlietfl- even if i initialize variables in controller, it provides same error. For eg. I initialized it in controller with $scope.details.data=2; and then tried to console. it didnt work.
you can't create an object that way ... $scope.details ={data:2}. If there is no object details you can't reference a property of it. Angular allows it in the view but it has to essentially do the same thing. This is standard javascript
@charlietfl- indeed, this was wat i was doing wrong. thank you for shedding some light into this.
@charlietfl- can you also let me know how to pass this data from controller back to the template now ? i mean, i want it to get initialized to the value set by me in the controller.

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.