gpt4 book ai didi

javascript - AngularJS 没有发生绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 07:56:11 25 4
gpt4 key购买 nike

我刚刚下载了 Visual Studio 的 AngularJS SPA 模板并开始了我的第一个应用程序。我已经面临很多问题了!!!

下面是我的 PersonView :

<!DOCTYPE html>

<html ng-app="app">
<head>
<meta name="viewport" content="width=device-width" />
<title>Add Person</title>
<script src="~/Scripts/vendor/angular.js"></script>
<script src="~/Scripts/app.js" type="text/javascript"></script>
<script src="~/Scripts/controllers.js"></script>

</head>
<body ng-controller="PersonCtrl">
<form name="A" ng-controller="PersonCtrl">
<div class="row" >
<div class="col-md-2">
First Name:
</div>
<div>
<input type="text" name="input" ng-model="$firstName"
ng-pattern="word" ng-required="true" />

</div>
</div>
</form>
<form name="B" ng-controller="PersonCtrl">
<div class="row" >
<div class="col-md-2">
Middle Name:
</div>
<div>
<input type="text" name="middleName" ng-model="$middleName"
ng-pattern="word" ng-required="true" />

</div>
</div>

</form>
<form name="C" ng-controller="PersonCtrl">
<div class="row" >
<div class="col-md-2">
Last Name:
</div>
<div>
<input type="text" name="lastName" ng-model="$lastName"
ng-pattern="word" ng-required="true" />

</div>
</div>
</form>



<br />

Name : {{firstName + middleName + lastName}}

下面是我的 Controller 类:

'use strict';

// Google Analytics Collection APIs Reference:
// https://developers.google.com/analytics/devguides/collection/analyticsjs/

angular.module('app.controllers', [])

// Path: /
.controller('HomeCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window)
{
$scope.$root.title = 'AngularJS SPA Template for Visual Studio';
$scope.$on('$viewContentLoaded', function () {
$window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title
});
});
}])

// Path: /about
.controller('AboutCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
$scope.$root.title = 'AngularJS SPA | About';
$scope.$on('$viewContentLoaded', function () {
$window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
});
}])

// Path: /login
.controller('LoginCtrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
$scope.$root.title = 'AngularJS SPA | Sign In';
// TODO: Authorize a user
$scope.login = function () {
$location.path('/');
return false;
};
$scope.$on('$viewContentLoaded', function () {
$window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
});
}])

.controller('PersonCtrl', ['$scope', function($scope) {
//$scope.$root.title = 'Create Person';
$scope.firstName = 'Aditya';
$scope.lastName = 'Swami';
$scope.middleName = ' ';

}])

// Path: /error/404
.controller('Error404Ctrl', ['$scope', '$location', '$window', function ($scope, $location, $window) {
$scope.$root.title = 'Error 404: Page Not Found';
$scope.$on('$viewContentLoaded', function () {
$window.ga('send', 'pageview', { 'page': $location.path(), 'title': $scope.$root.title });
});
}]);

还有我的 App.js

'use strict';


angular.module('app', ['ui.router', 'app.filters', 'app.services', 'app.directives', 'app.controllers'])

// Gets executed during the provider registrations and configuration phase. Only providers and constants can be
// injected here. This is to prevent accidental instantiation of services before they have been fully configured.
.config(['$stateProvider', '$locationProvider', function ($stateProvider, $locationProvider) {

// UI States, URL Routing & Mapping. For more info see: https://github.com/angular-ui/ui-router
// ------------------------------------------------------------------------------------------------------------

$stateProvider
.state('home', {
url: '/',
templateUrl: '/views/index',
controller: 'HomeCtrl'

})
.state('about', {
url: '/about',
templateUrl: '/views/about',
controller: 'AboutCtrl'
})
.state('login', {
url: '/login',
layout: 'basic',
templateUrl: '/views/login',
controller: 'LoginCtrl'
})
.state('person', {
url: '/PersonView',
layout: 'basic',
templateUrl: '/views/PersonView',
controller: 'PersonCtrl'
})
//.state('otherwise', {
// url: '*path',
// templateUrl: '/views/404',
// controller: 'Error404Ctrl'
//});

$locationProvider.html5Mode(true);

}])


.run(['$templateCache', '$rootScope', '$state', '$stateParams', function ($templateCache,$rootScope, $state, $stateParams) {

// <ui-view> contains a pre-rendered template for the current view
// caching it will prevent a round-trip to a server at the first page load
var view = angular.element('#ui-view');
$templateCache.put(view.data('tmpl-url'), view.html());

// Allows to retrieve UI Router state information from inside templates
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;

$rootScope.$on('$stateChangeSuccess', function (event, toState) {


// based on which page the user is located
$rootScope.layout = toState.layout;
});
}]);

我的解决方案结构如下所示:

enter image description here

最佳答案

尝试使用firstName而不是$firstName:

 <input type="text" name="input" ng-model="firstName" ng-pattern="word" ng-required="true" />

将您的 View 重写为(没有多个表单和 Controller ):

<body ng-controller="PersonCtrl">

<div class="row" >
<div class="col-md-2">
First Name:
</div>
<div>
<input type="text" name="input" ng-model="firstName"
ng-required="true" />

</div>
</div>

<div class="row" >
<div class="col-md-2">
Middle Name:
</div>
<div>
<input type="text" name="middleName" ng-model="middleName"
ng-required="true" />

</div>
</div>


<div class="row" >
<div class="col-md-2">
Last Name:
</div>
<div>
<input type="text" name="lastName" ng-model="lastName"
ng-required="true" />

</div>
</div>

同时删除 $templateCache 的完整代码,我认为它不是必需的。

关于javascript - AngularJS 没有发生绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26059844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com