Angular Js ng-controller没有填充该值

Angular Js ng-controller is not populating the value

本文关键字:填充 Js ng-controller Angular      更新时间:2023-09-26
可能是

之前提出的问题的副本,但请提供解决方案。我开始从头开始学习 Angular Js,所以请帮助获得正确的值。

我的 HTML 代码是

    <!doctype html>
<html lang="en" ng-app>
    <head>
        <meta charset = "utf-8">
        <title>Angular Js Test</title>
        <link rel="stylesheet" href= "bootstrap.min.css"/>
        <script src= "angular.min.js"></script>
        <script src="app.js"></script>
    </head>
    <body>  
        <div ng-controller="testController">
            {{appTest.title}}
        </div>
        <div id="header-wrapper" ng-controller="HeaderCtrl">
            <span class="logo pull-left">{{appDetails.title}}</span>
            <span class="tagline pull-left">{{appDetails.tagline}}</span>
            <div class="nav-wrapper pull-left">
                <ul class="nav nav-pills">
                    <li class="active"><a href="#">Books</a></li>
                    <li><a href = "#">Kart</a></li>
                </ul>
            </div>
            {{appDetails.title}}
        </div>
        <!--<p>Welcome to Bookart, we have collection of {{numofBooks + ' Million'}} books. </p>
        <input ng-model="numofBooks"/>
        <date-picker></date-picker>
        <div class="date-picker">test</div>-->
    </body>
</html>

和应用程序.js文件是

    var HeaderCtrl = function($scope){
    $scope.appDetails = {
        title: "BooKart",
        tagline: "We have 1 million books for you"
    };
}
var testController = function($scope){
    $scope.appTest = {
        title : "testTitle"
    };
}

在此之后,我无法从控制器获取动态值。

它不起作用,因为您使用的是Angular的默认"应用程序"。

首先创建应用。在您的"应用程序.js"中:

/*
 * CREATING THE APP
 * you will use "app" to create controller, services, directives etc...
 * 'appName' is the name of your app, it will be in the <html> tag
*/
var app = angular.module('appName', []);
/*
 * CREATING THE CONTROLLER HeaderCtrl
 * 'HeaderCtrl' the name of the controller
 * function($scope){} is the implementation
*/
app.controller('HeaderCtrl', function($scope) {
    $scope.appDetails = {
        title: "BooKart",
        tagline: "We have 1 million books for you"
    };
});
/*
 * CREATING THE CONTROLLER testController
*/
app.controller('testController', function($scope) {
    $scope.appTest = {
        title: "testTitle"
    };
});

然后在您的 html 代码中插入应用程序:

<html lang="en" ng-app="appName">