angular.js错误:未知提供者angular-seed

时间:2013-07-16 21:41:49

标签: angularjs seed directive

我在angular -s中开始了一个带有角种子的新项目。 我试图添加一些指令,但没有成功。

目标是,我的控制器中有2个选项/字段,并且希望为这些选择生成字段。

我总是得到错误:未知提供者:$ scopeProvider< - $ scope< - gendivDirective

是否需要依赖注射或其他什么? 还是错误的范围?控制器应该在指令中可用吗?

谢谢,帕特里克

的test.html:

<!doctype html>
<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="js/script.js"></script>
</head>
<body>

 <div ng-controller="FormCtrl">
        <span gendiv>
       </span>
    </div>
</body>
</html>

的script.js:

angular.module('myApp', ['myApp.directives','myApp.controllers']);

/* Controllers */

angular.module('myApp.controllers', [])
.controller('FormCtrl', ['$scope', function ($scope) {
$scope.choices = {
    object: [
        {label: "Choice0"},
        {label: "Choice1"}
    ]
};

$scope.addChoice = function() {
    $scope.choices.object.push({
        label: 'Choice' + $scope.choices.object.length.toString()
    });
};


}]);

/* directives */

angular.module('myApp.directives', [])
  .directive('gendiv', ['$scope', function($scope) {
return {
    controller: 'FormCtrl',
    template: '<div class="control-group" ng-repeat="choice in choices.object">' + 
               '<label class="control-label" for="{{choice.label}}">{{choice.label}}</label>' +
                '<div class="controls" style="color: #0044cc">' +
                    '<input type="text" placeholder="Enter second choice here" name="choices[]" data-ng-model="choices[]" required> <i class="icon-plus-sign icon-large" style="padding-left:10px;cursor: pointer" alt="add choice" ng-click="addChoice()"  class="add"></i>' +
                '</div>' +
                '<span ng-show="myForm.name.$error.required" class="help-inline">Required</span>' +
              '</div>',
    link: function (scope, elem, attrs) {
        console.log("Recognized the directive usage");
    }
}
  }]);

2 个答案:

答案 0 :(得分:2)

您无法将$ scope注入指令。无论如何它都会被传递给链接函数,所以你不应该这样做。我实际上并没有在你的代码中的任何地方看到你使用它。

答案 1 :(得分:0)

请记住,您正在重置控制器......

angular.module('myApp', ['myApp.directives','myApp.controllers']);

/* Controllers */

angular.module('myApp.controllers', []) // YOU ARE RESETTING YOUR CONTROL TRY THIS

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