0

I am new to angular js. I am trying out an example from http://egghead.io/lessons/angularjs-directives-talking-to-controllers but somehow it does not seem to work correctly for me.

here is my html

<!DOCTYPE html>
 <html lang="en">
    <head>
    <meta charset="utf-8" />
    <title></title>
    <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
    <script src="js/twitterApp.js"></script>
</head>
<body>
    <div ng-app="twitterApp">
        <div app-controller="AppCtrl">
            <div enter="loadMoreTweets()">Roll over to load tweets.</div>
        </div>
    </div>
</body>
</html>

here is app, controller and directive

var tApp= angular.module("twitterApp",[])

tApp.controller("AppCtrl", function ($scope) {
   $scope.loadMoreTweets = function () {
      alert('Loading tweets.');
   }
})

 tApp.directive("enter", function () {
     return function (scope, element, attrs) {
        element.bind("mouseenter", function () {
            scope.$apply(attrs.enter);    
        })
     }
 })

the problem is below statement seems to be failing and I can't figure out the reason since I did exactly the way it is done in demo.

scope.$apply(attrs.enter)

I even tried following but error console displays loadMoreTweets is not found, any help is greatly appreciated.

scope.loadMoreTweets()
2
  • If you console.log(attrs.enter) what do you see in the console? Commented Oct 30, 2013 at 14:57
  • It correctly displays the attribute value "loadMoreTweets()" but the problem was controller did not register due to my mistake. I could not figure that out because scope.$apply did not throw any error. Commented Oct 31, 2013 at 6:17

1 Answer 1

2
<div app-controller="AppCtrl"> 

should be

<div ng-controller="AppCtrl">
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for pointing out the error. The controller did not register but the behavior did register correctly so while accessing scope inside the mouseenter event it kept giving an error loadMoreTweets is not found.

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.