简体   繁体   English

ng-app并不总是启动

[英]ng-app not always starting

I started having problems in my new project, where I'm using angular apps in pyramid served jinja2 templates. 我在新项目中开始遇到问题,我在金字塔服务jinja2模板中使用角度应用程序。

One template has DIV with ng-app that starts working only sometimes. 一个模板具有带有ng-app的 DIV,该模板有时仅会开始工作。 I tried with simple module with one controller that only logs "I'm here!" 我尝试使用带有一个仅记录“我在这里!”的控制器的简单模块。 into console, but it worked only sometimes (I'm 100% sure template was rendered correcly). 进入控制台,但它仅在某些情况下有效(我100%确保正确渲染了模板)。

My workaround for this problem is to use ng-app tag and to use angular.bootstrap at the same time, which sometimes generates errors, but makes app work. 我针对此问题的解决方法是同时使用ng-app标签和angular.bootstrap ,这有时会产生错误,但会使应用程序正常工作。 For whatever reason just using angular.bootstrap does not always work. 出于任何原因,仅使用angular.bootstrap并不总是有效。

Am I missing something here? 我在这里想念什么吗?

 var ticketApp = angular.module('ticketApp', ['ngRoute', 'ui.bootstrap']); ticketApp.factory('ticketAppData', function() { var all_ticket_data = {}; return {all_ticket_data: all_ticket_data}; }); ticketApp.controller('TicketAppController', function($scope, ticketAppData) { }); ticketApp.controller('TicketAppListController', function($scope, ticketAppData) { $scope.data = ticketAppData; }); ticketApp.controller('TicketAppDetailsController', function($scope, ticketAppData) { $scope.data = ticketAppData; }); ticketApp.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/list', { templateUrl: '/static/ticket-ng-app/views/list.html', controller: 'TicketAppListController' }). when('/details/:ticket_id', { templateUrl: '/static/ticket-ng-app/views/details.html', controller: 'TicketAppDetailsController' }). otherwise({ redirectTo: '/list' }); } ]); angular.bootstrap($('#ticketApp-window'), ['ticketApp']); 
 <div id="ticketApp-window" ng-app="ticketApp" ng-controller="TicketAppController"> <div ng-view class="ticket-app-view"></div> </div> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular-route.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.12.0/ui-bootstrap.js"></script> <script src="../../static/ticket-ng-app/app.js"></script> 

Try removing ng-app, putting the controller on the ng-view and calling angular.bootstrap like this: 尝试删除ng-app,将控制器放在ng-view上,然后按以下方式调用angular.bootstrap:

var ticketAppWin = document.getElementById('ticketApp-window');
angular.bootstrap(angular.element(ticketAppWin), ['ticketApp']);

It's better to let angular.element wrap up the DOMElement than doing it yourself. 最好让angular.element包装DOMElement比自己完成。

Check out this fiddle: http://jsfiddle.net/u78meq8g/ 看看这个小提琴: http : //jsfiddle.net/u78meq8g/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM