AngularJS未知提供程序(filterProvider)错误

时间:2014-01-29 22:07:19

标签: javascript angularjs

下面的两个脚本语句独立工作,但是当它们合并时会导致错误:[$ injector:unpr]未知提供者:searchNameFilterProvider< - searchNameFilter 任何人都可以解释为什么会发生这种情况吗?

第1段

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>

2 个答案:

答案 0 :(得分:1)

<div ng-app="myApp">
    <script src="Scripts/Angular.js" type="text/javascript"></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>

答案 1 :(得分:1)

代码中应该只有一个模块初始化。修复重复的初始化可以解决问题。

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

以下是AngularJS文档中的文档。

&#34; 传递一个参数检索现有的angular.Module,而传递多个参数会创建一个新的angular.Module&#34;

您可以在此处阅读有关模块的更多信息。 AngularJS Modules

&#13;
&#13;
var app = angular.module('myApp', []);
app.filter('searchName', function() {
  return function(input) {
    return input + '!';
  }
});

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

  $scope.myFunc = function() {
    console.log('Hello world!');
  };
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
  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>

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