【发布时间】:2016-04-18 10:57:12
【问题描述】:
我正在使用此代码在数组中查看和添加人员。
index.html
<!DOCTYPE html>
<html ng-app="myApp">
<body>
<h1>People</h1>
<div ng-controller="Ctrl">
<div ng-view></div>
</div>
<script src="angular.min.js"></script>
<script src="angular-route.min.js"></script>
<script src="controller.js"></script>
</body>
</html>
add.html
<h1>Add</h1>
<input type="text" ng-model="new_name" placeholder="Name">
<br />
<input type="text" ng-model="new_age" placeholder="Age">
<br />
<button ng-click="add()">Add</button>
<hr />
<a href="#/">Back</a>
controller.js
var app = angular.module('myApp', ['ngRoute']);
app.config(function ($routeProvider){
$routeProvider
.when('/',{
templateUrl: 'list.html'
})
.when('/add',{
templateUrl: 'add.html',
});
});
app.controller('Ctrl', function($scope) {
$scope.people = [{'name':'Alex', 'age':25}, {'name':'Dan', 'age': 25}];
$scope.add = function(){
$scope.people.push({'name':$scope.new_name, 'age':$scope.new_age});
$location.path("/");
};
});
问题是,我没有从ng-model="new_name" 获取数据到控制器方法$scope.add = function()。按下添加按钮后,只有空白字符串被推入数组。但是,模型和控制器可以在没有路由和 ng-view 指令的情况下完美运行。我认为它的范围相关问题。任何机构都可以帮助我。
谢谢。
【问题讨论】:
-
什么是app目录结构?
-
所有文件都在同一个目录中。
标签: javascript angularjs angularjs-scope