0

What is wrong here?

JSFiddle.

function SecondCtrl($scope, Data) {
  $scope.data = Data;

  $scope.reversedMessage = function(message) {
    return message.split("").reverse().join("");
  };
}
2
  • 1
    you are missing many steps... like there is no module created... the controller is not added to the module... there is no provider for Data Commented Jan 15, 2014 at 6:13
  • a simple sample with a hard coded data jsfiddle.net/arunpjohny/mH5uH/1 Commented Jan 15, 2014 at 6:18

2 Answers 2

2

As Arun mentioned in his comment, you're missing several key elements here:

  1. You're not bootstrapping your app. You'll need to use either the ng-app directive or angular.bootstrap.

  2. Since you're defining SecondCtrl as a global function (which isn't a best practice), you need to set JSFiddle to load your JavaScript before onLoad; I used No wrap - in <head>:

    screenshot

  3. You're injecting Data into your controller, but you haven't defined Data as a service. You'll need to create a service for this.

Here's a JSFiddle that demonstrates how things might look if you follow best practices and create a module for your controller in addition to fixing the other issues: http://jsfiddle.net/BinaryMuse/TcPGT/

<div ng-app='myApp'>
  <div ng-controller="SecondCtrl">
    <input type="text" ng-model="data.message">
    <h1>{{ reversedMessage(data.message) }}</h1>
  </div>
</div>
var app = angular.module('myApp', []);

app.value('Data', {
  message: 'This is my message.'
});

app.controller('SecondCtrl', function($scope, Data) {
  $scope.data = Data;

  $scope.reversedMessage = function(message) {
    return message.split("").reverse().join("");
  };
});
Sign up to request clarification or add additional context in comments.

Comments

0

Your code i think is incomplete..Iam Attaching a complete code here..Go through it and i hope this is what your looking for.

Visit :http://plnkr.co/edit/HxScwWRT9nnaA5CILE6f?p=preview

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.