0

Im new to AngularJS, and cannot figure why I can not get a response to my button click.Any help would be much appreciated. Ive looked at other examples of a controller being used but can not see where I'm going wrong.

Edited: I have two scripts which work independent however when combined cause this Error: [$injector:unpr] Unknown provider: searchNameFilterProvider <- searchNameFilter

   <html lang="en" ng-app="myApp" >
    <head>
      <meta charset="utf-8">
      <title>My AngularJS App</title>
      <link rel="stylesheet" href="css/app.css"/>
    </head>
    <body>
        <!-- In production use:
      <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
      -->
      <script src="lib/angular/angular.js"></script>
      <script src="lib/angular/angular-route.js"></script>
      <script src="js/app.js"></script>
      <script src="js/services.js"></script>
      <script src="js/controllers.js"></script>
      <script src="js/filters.js"></script>
      <script src="js/directives.js"></script>

    Find Person: <input type="text" ng-model="myName">
    <ul ng-init="people = ['Diarmuid','Aine','Dave','Declan']">
        <li ng-repeat="person in people | filter:myName">{{ person | searchName}}</li>
    </ul>

    <script>
        var app = angular.module('myApp',[]);
        app.filter('searchName',function(){
            return function (input){
                return input + '!';
            }
        })
    </script>

    <div ng-controller="myCtrl">
        <button ng-click="myFunc()">Hello World Button</button>
    </div>

    <script>

        var app = angular.module('myApp',[]);
        app.controller('myCtrl',function ($scope) {

            $scope.myFunc = function () {
               console.log('Hello world!');
            };
        });

    </script>
4
  • You shouldn't call alert inside angular the correct is injecting $window service and then $window.alert Commented Jan 29, 2014 at 20:48
  • 3
    The correct way is not using alert at all. instead use console.log it's 2014! Commented Jan 29, 2014 at 20:56
  • @IlanFrumer agreed :) Commented Jan 29, 2014 at 20:57
  • I also wouldn't use $window.console.log ! that is just pointless as it's used only for debugging. The whole point of $window and other $wrappers is to make mocking easy inside tests or to add functionality, Commented Jan 29, 2014 at 21:01

3 Answers 3

2

Works ok http://jsfiddle.net/davekr/tWM2U/

Maybe it's because you have typo in your html. You missed the > in <script src="js/directives.js"></scrip

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

Comments

1

You've got your HTML and scripts all mixed up a bit.

How about using this Codepen example : http://codepen.io/calendee/pen/ysCem

<html lang="en" ng-app="myApp" >
<head>
    <meta charset="utf-8">
    <title>My AngularJS App</title>
    <link rel="stylesheet" href="css/app.css"/>

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
</head>
<body>


<div ng-controller="myCtrl">
    <button ng-click="myFunc()">Hello World Button</button>
    <p>Button Clicked {{clickCounter}} times</p>
</div>

<script>

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

    app.controller('myCtrl',function ($scope) {

        $scope.clickCounter = 0;

        $scope.myFunc = function () {

            alert('Hello world!');
            $scope.clickCounter++;
        };
    });

</script>

</body>
</html>

Comments

0

Correct me if I am wrong but I test your code in this plunker and it workd fine on my end:

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="http://code.angularjs.org/1.0.7/angular.js" data-semver="1.0.7"></script>       
  </head>
<body>


<div ng-controller="myCtrl">
        <button ng-click="myFunc()">Hello World Button</button>
    </div>

    <script>

        var app = angular.module('myApp',[]);
        app.controller('myCtrl',function ($scope) {

            $scope.myFunc = function () {
                alert('Hello world!');
            };
        });

    </script>

1 Comment

Your right its an error caused by other code when this script is added

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.