Angular.js中的配置模块

时间:2013-12-21 23:22:37

标签: javascript angularjs

我是angujar.js的新手,我想开发一个简单的应用程序,使用某些html文件来更改内容,例如this example

我的配置错了吗?还有什么我需要做的?

的index.html

<html ng-app="main">
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> 
</head>

<body>
    Some title
    <div ng-view></div>
</body>

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js">
<script src="js/main.js"></script> <!-- app -->

</html>

main.js

var mymain = angular.module('main',[]);

mymain .config(  function($routeProvider) {
    $routeProvider.
      when('/', {
        templateUrl: 'otherfile.html',
        controller: 'F'
      }).
      otherwise({
        redirectTo: '/'
      });
  });

mymain.controller('F', function($scope) { 
    alert("here");    
});

otherfile.html

<span>Something else...</span>

提前致谢。

被修改

我是否必须在服务器上拥有angular.js(至少是localhost)?我的路由代码here,但不是我用浏览器打开文件时。

2 个答案:

答案 0 :(得分:1)

路由器不是核心angular.js文件的一部分。它是一个单独的模块,必须在主模块依赖项中声明。它也在一个单独的JS文件中,必须添加到index.html文件中,如the documentation中所述。

答案 1 :(得分:0)

JB Nizet 是对的,我需要

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-resource.min.js"></script>

但我也需要

<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular-route.min.js"></script>