Angular ReferenceError:未定义Controller

时间:2015-05-09 15:01:37

标签: angularjs

我这里有一个傻瓜 - http://plnkr.co/edit/ezKOtG9KJ6nD0068jpry?p=preview

我在这里遵循这个简单的角度教程 - https://www.youtube.com/watch?v=aG8VD0KvUw4

当我运行代码时,获取ReferenceError:Controller未定义

任何人都可以解释这个或谁解决它。

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

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

            $scope.sheildNames = [];

            this.addReigns = function(){

                $scope.shieldNames.push('Reigns: One');

            };

            this.addCollins = function(){

                $scope.shieldNames.push('Collins: Two');

            };

            this.addAmbrose = function(){

                $scope.shieldNames.push('Ambrose: Three');

            };

        })


        .directive('theshield', function(){

            return{
                restrict: 'E',
                scope: {},
                controller: 'ShieldCtrl',
                link: function(scope, element, attrs){

                    element.bind('mouseenter', function(){

                        console.log(scope.sheildName);

                    })
                }
            }
        })


        .directive('reigns', function(){
            return{
                require: 'theshield',
                link: function(scope, element, attrs, ShielCtrl){
                    ShieldCtrl.addReigns();
                }
            }
        })

        .directive('collins', function(){
            return{
                require: 'theshield',
                link: function(scope, element, attrs, ShielCtrl){
                    ShieldCtrl.addCollins();
                }
            }
        })

        .directive('ambrose', function(){
            return{
                require: 'theshield',
                link: function(scope, element, attrs, ShielCtrl){
                    ShieldCtrl.addAmbrose();
                }
            }
        })

1 个答案:

答案 0 :(得分:1)

你有一个简单的拼写错误,你的指令控制器依赖是ShielCtrl,你正在尝试使用ShieldCtrl。

    .directive('reigns', function(){
        return{
            require: 'theshield',
            link: function(scope, element, attrs, ShielCtrl){
                ShieldCtrl.addReigns(); <-- CHECK spelling.
            }
        }
    })

您的下一个错误是另一个拼写错误:$ scope。 sheildNames = [];并且您尝试访问正确的拼写:$ scope。 shieldNames .push('Reigns:One');