0

I am new to Angular js. I have tried the following code:

<html>
<head>
    <title>angular js</title>
    <script src="js/lib/jquery.min.js"></script>
    <script src="js/lib/angular.min.js"></script>
</head>
<body ng-app="MyApp" ng-controller="MyController">
    <input ng-model="data.selected">
    <h1>{{data.selected}}</h1>


    <script>
    var app = angular.module('MyApp',[]);
    app.controller('MyController',function($scope){
        $scope.data.selected = "Initial Text";
    });

    </script>
</body>
</html>

I tried this code to show the initial text in the H1 tag. But I got the error $scope.data is undefined What is wrong here? How can I solve this?

2
  • 1
    in the controller you're accessing the field "selected" of something undefined. try $scope.data = {}; $scope.data.selected="initial text" Commented Oct 21, 2013 at 10:37
  • 2
    or $scope.data = {selected: "Initial Text"}; Commented Oct 21, 2013 at 10:39

2 Answers 2

3

This isn't an AngularJS-specific issue, but a general JavaScript error.

in the controller you're accessing the field "selected" of something undefined. try

$scope.data = {}; 
$scope.data.selected="initial text"

or

$scope.data = {selected : "initial text"}; 

http://jsfiddle.net/egamonal/Fn5KF/1/

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

Comments

0

try $scope.data = {"selected":"Initial Text"};

Comments

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.