2014-09-26 37 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}}