2017-02-09 17 views
0

我是angularJS的新手,並試圖讓下面的簡單示例工作。但是當我跑它,我得到了一個空白的屏幕,而不是「你好世界」。幫助將不勝感激。謝謝。使用angularJS .component()但沒有值

角comp.js:

angular.module('myApp').component('greetUser', { 
     template: 'Hello, {{$ctrl.user}}!', 
     controller: function GreetUserController() { 
      this.user = 'world'; 
     } 
}); 

的index.html:

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
    <meta charset="ISO-8859-1"> 
    <title>AngularJS</title> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js">    </script> 
    <script src="js/angularcomp.js"></script> 
</head> 
<body> 
    <greet-user></greet-user> 
</body> 
</html> 

更新:我發現這個問題,版本1.4.5不支持組件。我現在使用1.6.1,它的工作原理!

+0

我工作的這個..等一下 – thiagoh

+1

你可能是指'angular.module( '對myApp',[])',而不是'angular.module('對myApp 「)'。 –

回答

1

這裏的問題

angular.module('myApp')

駐留的位置,這應該是

angular.module('myApp',[])

,因爲你正在創建一個新的模塊。如果你沒有傳遞第二個數組參數,AngularJS會嘗試找到你的模塊,而不是創建一個新模塊。

試試這個:

<!DOCTYPE html> 
<html> 

    <head> 
    <meta charset="ISO-8859-1"> 
    <title>AngularJS</title> 
    </head> 

    <body ng-app="myApp"> 
    <greet-user></greet-user> 
    </body> 

    <script> 
    angular.module('myApp',[]) 
     .component('greetUser', { 
     template: 'Hello, {{$ctrl.user}}!', 
     controller: function GreetUserController() { 
      this.user = 'world'; 
     } 
     }); 

    </script> 

</html> 
+0

- thiagoh我嘗試瞭解決方案,但仍未在網頁上看到「Hello world」。 – jlp

+0

我在IDE中複製了上述解決方案,並再次運行它,但仍未在網頁上看到「Hello world」。 – jlp

+0

您是否包含角1的最後一個版本? – thiagoh

相關問題