2014-09-26 60 views
1

我刚刚下载了Visual Studio的AngularJS SPA模板,并开始使用我的第一个应用程序。我已经面临很多问题了!绑定没有发生与AngularJS

下面是我的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}} 

下面是我的控制器类:

'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; 
    }); 
}]); 

我这样下面lution结构如下所示:

enter image description here

+0

这将是有益的,如果你创建一个plunker/jsfiddle等这个 – Dreamwalker 2014-09-26 12:42:53

+0

我不确定这个!我只是创建我的第一个应用程序。我读过帖子,并建议使用此模板。所以继续与这 – adityaswami89 2014-09-26 12:44:36

+0

对不起,忽略该评论我虽然它是纯js和HTML :) – Dreamwalker 2014-09-26 13:08:50

回答

0

尝试使用的firstName而不是$姓:

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

重写视图(不多种形式和控制器):

<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的完整代码,我不认为这是必需的。

+0

dint解决。仍然是同样的问题。 – adityaswami89 2014-09-26 12:54:03

+0

@ adityaswami89为什么你在PersonView上有4次PersonCtrl?它应该只有一个。 – 2014-09-26 12:58:42

0

你的文件包含已经在父文件中的东西。

这一切都应该被删除(就是你没有张贴的所有文件从文件末尾删除</body></html>标签)

<!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"> 

文件中还要注意你嵌套的控制器,该控制器实际上已经在路由上定义了,所以在任何地方都不需要ng-controller。

有没有指向多个form标签?你可能有一个特定的要求,但否则我会用一个表单包装所有的页面。

编辑

你也应该预先绑定的控制器领域与$

下面是完整的文件应该是什么样子的建议。

<form name="MyForm" novalidate> 
    <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> 

    <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> 

    <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}}