假设我有这个模板:
{% load staticfiles %}
{% load facebook %}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Autos Queridos | Muéstranos cuánto sabes</title>
<link rel="stylesheet" type="text/css" href="{% static 'css/index.css' %}" />
<script type="text/javascript" src="{% static 'js/angular.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/angular-ui-router.min.js' %}"></script>
<script type="text/javascript" src="{% static 'js/index.js' %}"></script>
{% facebook_init %}
_FacebookSrv.initialize();
{% endfacebook %}
<script type="text/javascript">
AutosQueridos.constant('AutosQueridos.StaticUrl', '{{ STATIC_URL }}');
angular.bootstrap(document, ['AutosQueridos']);
</script>
</head>
<body ng-controller="AutosQueridos.Base">
{% verbatim angular %}
<div ui-view>
<!-- aca es donde va toda la aplicacion -->
</div>
{% endverbatim angular %}
</body>
</html>
让我们分析一下:
angular.bootstrap
是手动调用的,因为我需要预先将STATIC_URL
配置为一个服务。这种常量服务是在调用之前定义的。_FacebookSrv.initialize();
是一个定制的调用,我稍后将对其进行描述。它是在window.fbAsync
上下文中执行的(它不与角对象的正常流直接交互,也不与任何作用域,也不与DOM)。body
有一个控制器,我们稍后再讨论(这是问题的焦点)。ui-view
指令,因为我使用ui.router作为状态处理程序库。让我们来描述一下在这方面不应该注意的事情:
{% load xxx %}
标记根本不生成任何输入。{% verbatim xxx %}
/{% endverbatim xxx %}
根本不生成输入,而是特定于框架的标记。{% static xxx %}
标记的目的是定位实际的文件路径。因此,没有什么可担心的标签-它们是Django-特定的,并按照预期的行为。
css/index.css
文件还没有任何内容。角码位于js/index.js
AutosQueridos = angular.module('AutosQueridos', ['ui.router']);
var _FacebookSrv = {
/* ... */
initialize: function() {
/* ... */
},
/* ... */
};
/* contents of _FacebookSrv are trimmed to narrow the problem. it is used by AutosQueridosFB and the initialize() has not met the conditions to do anything useful yet, with the code I wrote in my template */
var AutosQueridosFB = function($scope) {
this.$scope = $scope;
};
/* methods of AutosQueridosFB were trimmed to narrow the problem. this factory is not yet used*/
AutosQueridos_Facebook = AutosQueridos.factory('AutosQueridos.Facebook', ['$scope', function($scope){ return new AutosQueridosFB($scope); }]);
AutosQueridos_Base = AutosQueridos.controller('AutosQueridos.Base', ['$state', '$rootScope', function($state, $rootScope){
/* pay attention to THIS controller */
console.log('nestor');
$state.go('welcome');
}]);
AutosQueridos_Welcome = AutosQueridos.controller('AutosQueridos.Welcome', ['$scope', '$state', '$rootScope', function($scope, $state, $rootScope){
/* there's nothing yet in these controllers */
}]);
AutosQueridos_Roulette = AutosQueridos.controller('AutosQueridos.Roulette', ['$scope', '$state', '$rootScope', '$http', function($scope, $state, $rootScope, $http){
/* there's nothing yet in these controllers */
}]);
AutosQueridos_Trivia = AutosQueridos.controller('AutosQueridos.Trivia', ['$scope', '$state', '$rootScope', '$http', function($scope, $state, $rootScope, $http){
/* there's nothing yet in these controllers */
}]);
AutosQueridos.config(['$stateProvider', 'AutosQueridos.StaticUrl', function($sp, url) {
//
// Now set up the states
$sp
.state('welcome', {
templateUrl: url + "partials/welcome.html",
controller: 'AutosQueridos.Welcome'
})
.state('roulette', {
templateUrl: url + 'partials/roulette.html',
controller: 'AutosQueridos.Roulette'
})
.state('trivia', {
templateUrl: url + 'partials/trivia.html',
controller: 'AutosQueridos.Trivia'
})
}]);
我的问题是:从未执行body
中的AutosQueridos.Base
控制器:我没有看到控制台中显示'nestor‘,也没有看到“欢迎”状态被反映(状态模板有哑内容,比如“欢迎!”)。我错过了什么
更新的:我没有得到任何4xx/5xx错误-文件按预期加载。我在控制台中也没有得到javascript错误。
发布于 2014-08-29 12:35:09
只有在加载了所有模块并准备好文档之后,您才需要启动引导。因此,将您的函数打包到文档中,如下所示:-
angular.element(document).ready(function() {
angular.bootstrap(document, ['AutosQueridos']);
});
这是您的代码应该遵循的顺序:
https://stackoverflow.com/questions/25575548
复制相似问题