admin管理员组文章数量:1313094
I am using Angular Js
for front-end of my sample, created a template and loaded views dynamically according to requirement. I am using angular
, jquery
and cusotm js
for my sample application. My templates worked fine and basic structure is as follows:
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
---------------- bootstap, css etc ----------- stylesheet include
<script src='assets/js/angular.min.js' type="text/javascript"></script>
</head>
<body>
<div ng-view></div>
</body>
-------------------- jquery, bootstrap ---------- javascript include
<script src='lib/angularjs/angular-route.js' type="text/javascript"></script>
<script src='lib/angularjs/custom-function.js' type="text/javascript"></script>
When the application runs, all my script and style-sheets are loaded successfully. But in my custom-function.js
, I am using, elements id
and classes
for performing some work. When the custom-function.js
loaded there are no document structure is defined, because the document is loaded dynamically using <div ng-view></div>
.
I am also trying to include <script src='lib/angularjs/custom-function.js' type="text/javascript"></script>
in my document, which is dynamically loaded by angular, but custom-function.js
function is not running.
I am new in angular, I searched google, but still not find any appropriate solution. how could I solve this?
I am using Angular Js
for front-end of my sample, created a template and loaded views dynamically according to requirement. I am using angular
, jquery
and cusotm js
for my sample application. My templates worked fine and basic structure is as follows:
<!doctype html>
<html lang="en" ng-app="myApp">
<head>
---------------- bootstap, css etc ----------- stylesheet include
<script src='assets/js/angular.min.js' type="text/javascript"></script>
</head>
<body>
<div ng-view></div>
</body>
-------------------- jquery, bootstrap ---------- javascript include
<script src='lib/angularjs/angular-route.js' type="text/javascript"></script>
<script src='lib/angularjs/custom-function.js' type="text/javascript"></script>
When the application runs, all my script and style-sheets are loaded successfully. But in my custom-function.js
, I am using, elements id
and classes
for performing some work. When the custom-function.js
loaded there are no document structure is defined, because the document is loaded dynamically using <div ng-view></div>
.
I am also trying to include <script src='lib/angularjs/custom-function.js' type="text/javascript"></script>
in my document, which is dynamically loaded by angular, but custom-function.js
function is not running.
I am new in angular, I searched google, but still not find any appropriate solution. how could I solve this?
Share Improve this question edited Mar 20, 2015 at 4:25 Kailas 7,5783 gold badges50 silver badges65 bronze badges asked Mar 19, 2015 at 13:08 Harmeet Singh TaaraHarmeet Singh Taara 6,61122 gold badges75 silver badges127 bronze badges 5- You custom-function.js should be a controller, directive or service inside angular, otherwise it's going to be hard to make it work. You should 'translate' you jquery code in an angular way of programming – Norbor Illig Commented Mar 19, 2015 at 13:11
-
You can put the related script tag
<script src='lib/angularjs/custom-function.js' type="text/javascript"></script>
to end of your view template which loaded in ng-view. – okanozeren Commented Mar 19, 2015 at 13:25 - Hello @nerezo, we already try this, the script is loaded, but not working – Harmeet Singh Taara Commented Mar 19, 2015 at 13:27
- @harmeet-singh-taara can you provide an example about what code is in your custom script? – okanozeren Commented Mar 19, 2015 at 13:33
-
@nerezo the method like
$(document).ready(function() { }
function for documents elements. – Harmeet Singh Taara Commented Mar 19, 2015 at 13:43
3 Answers
Reset to default 3With helps of ngRoute a template can be loaded which is containing a script tag which includes the js file contains javascript codes that we wanted to run.
Please take a look the below example for usage:
http://embed.plnkr.co/AonatjhKlPMaOG6vVWW1/preview
This sample might help you to find some solution using 'ngRoute' module to load what you want to show in your 'ng-view' you can give your instructions in .js file.
<script src="yourinstruction.js"></script>
</head>
<body ng-app='base'>
<div ng-view></div>
<script>
var router = angular.module('base', ['ngRoute']);
router.config(['$routeProvider',
function($routeProvider) {
$routeProvider.when('/', {
templateUrl: '/home.html',controller:'test'
}).otherwise({
redirectTo: '/'
});
}]);
</script>
<!--other codes-->
yourinstruction.js file
router.controller('test',function ($scope) {
<!--your instructions-->
})
});
You can solve this issue using '$viewContentLoaded' as shown in this tutorial: http://www.aleaiactaest.ch/2012/06/28/angular-js-and-dom-readyness-for-jquery/
Another way is by dependency injection (the Angular way). The first part of this tutorial show us how to include an external lib by using dependecy injection. http://www.ng-newsletter./posts/d3-on-angular.html
本文标签: jqueryAngular JS load custom javascript file in ngviewStack Overflow
版权声明:本文标题:jquery - Angular JS: load custom javascript file in ng-view - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741947744a2406524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论