簡體   English   中英

如何使它們一起加載並准備就緒

[英]How make that works load and ready together

我有這段代碼...我在<head>加載了許多files.js,效果很好...但是... {{sec-intro}}是一個變量,具有其值,但不會加載文件。 ..

<!-- language: lang-js -->
<script type="text/javascript">
    $( document ).ready(function() {
        jQuery('head').load( 'tpl/head.html' );
    });
</script>

<script type="text/javascript">
    $( document ).ready(function() {
        jQuery('#intro').load( 'tpl/sec-intro-{{sec-intro}}.html' );
    });
</script>

我不確定是否知道您的意思,但是如果sec-intro是一個javascript變量,您可以嘗試以下方法:

<script type="text/javascript">
    $( document ).ready(function() {
        jQuery('#intro').load( 'tpl/sec-intro-' + sec-intro + '.html' );
    });
</script>

但是我認為您對使用{{var}}的AngularJS表示法感到困惑。

編輯:

好吧,首先,變量名稱不能帶有連字符,因此您需要更改該名稱。

第二件事,您應該在控制器的代碼中聲明該函數。 我做了一個小角度的應用程序,它是代碼:

腳本:

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.secintro = '01';

  $scope.loadFunction = function(){
    jQuery('#intro').load('tpl/sec-intro-' + $scope.secintro + '.html');
  };

  $scope.loadFunction();
});

HTML:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="jquery" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script data-require="angular.js@1.0.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js" data-semver="1.0.8"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">

  </body>

</html>

然后,您可以將loadFunction用作angularJS函數,即:ng-init =“ loadFunction();”

 <body ng-controller="MainCtrl" ng-init="loadFunction();">

  </body>

這將在控制器准備就緒時執行該功能,因此您不需要$scope.loadFunction(); 初始化。

這是pluker,僅在控制台中打印正確的字符串。

我希望這最終對您有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM