2016-06-08 109 views
0

我很新的角度js和下面的错误是我的代码

<script type="text/javascript" src="node_modules/angular/angular.min.js"></script> 

    <script type="text/javascript"> 
     (function(angular){ 

      var testAngular = angular.module('testAngular'); 

      testAngular.controller = ("name_controller", function($scope) {console.log("hello"); 
       $scope.name = { 
        firstName: "null", 
        lastName: "null", 
        setName: function(fname, lname) { 

         if(fname.trim != "") { 
          this.firstName = fname; 
         } 

         if(lname.trim()!="") { 
          this.lastName = lname; 
         } 
        }, 
        getName: function() { 
         var name_object = $scope.name; 
         return name_object.firstName+" "+name_object.lastName; 
        } 
       }; 
      }); 
     })(window.angular); 
    </script> 

    <div ng-app="testAngular" ng-controller="name_controller"> 
     Enter first name: <input type="text" ng-model="name.firstName"><br><br> 
     Enter last name: <input type="text" ng-model="name.lastName"><br> 
     <br> 
     You are entering: {{ name.firstName }} 
    </div> 

现在,当我试图运行此代码我“M获得在控制台2级的错误作为

[$injector:nomod] 

[$injector:modulerr] 

任何想法,到底为什么这种情况正在发生。一些职位说我需要包括路由模块,但我没有使用路由在我的代码中的任何地方。

回答

1

尝试改变:

testAngular.controller = ("name_controller", function($scope) {

testAngular.controller("name_controller", function($scope) {

+0

大。这工作..只是感觉好奇,我发现了“testAngular.controller =(”name_controller“,函数($范围){”方式在一个职位,但什么问题? –

+1

该控制器是一个函数,接受名称和构造函数 您可以在这里阅读更多https://docs.angularjs.org/guide/controller 和https://docs.angularjs.org/api/ng/service/$controller –

相关问题