Angularjs 范围错误
Angularjs scope error
需要一些关于 angular 的帮助,我对此一无所知,但我有工作要做 ^^'。我的问题是当我写入我的输入时,我无法获得 ng-model return 并得到一个范围错误告诉 $scope.newemployee is undefined
(更正)。
新问题我的 table 'List' 像我的对象 employee
一样保持空白
<!DOCTYPE html>
<html ng-app="employe">
<head>
<meta charset="utf-8" />
<title>
List of employe
</title>
<meta name="viewport" content="with=device-width , initial-scale=0 ,shrink- to-fit=no" />
<link rel="stylesheet" href="./files/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="./files/style.css" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="./files/code.js">
</script>
</head>
<body>
<div ng-controller="FormController as FormCtrl">
<button ng-click="FormCtrl.setShow(2)">Add new employee</button>
<form class="form-horizontal" ng-controller="ListCtrl" name="infoForm" ng-submit="addEmployee()" ng-show="FormCtrl.isShow(2)" novalidate="" id="infoForm">
{{newemployee}}
<h2>
Add/Edit employee
</h2><!-- NOM -->
<div class="form-group">
<label for="name" class="col-xs-5 control-label">Name</label>
<div class="col-xs-2">
<input type="texte" ng-model:"newemployee.name" class="form-control" ng-required="true" name="name" />
</div>
</div><!-- AGE -->
<div class="form-group">
<label for="age" class="col-xs-5 control-label">Age</label>
<div class="col-xs-2">
<input type="number" ng-model:"newemployee.age" class="form-control" ng-required="true" />
</div>
</div><!-- NICKNAME -->
<div class="form-group">
<label for="nickname" class="col-xs-5 control-label">Nickname</label>
<div class="col-xs-2">
<input type="texte" ng-model:"newemployee.nickname" class="form-control" ng-required="true" />
</div>
</div><!-- EMPLOYEE -->
<div class="form-group">
<label for="employee" class="col-xs-5 control-label">Employee</label>
<div class="col-xs-2">
<label class="radio-inline"><input type="radio" name="inlineRadioOptions" id="Yes" ng-model:"newemployee.yes" value="yes" />Yes</label> <label class="radio-inline"><input type="radio" name="inlineRadioOptions" id="No" ng-model:"newemployee.no" value="no" />No</label>
</div>
</div><!-- JOB -->
<div class="form-group">
<label for="job" class="col-xs-5 control-label">Job</label>
<div class="col-xs-2">
<select ng-model:"newemployee.job" class="form-control">
<option>
Founder
</option>
<option>
Market chef
</option>
<option>
Stage
</option>
</select>
</div>
</div><!-- ANNEE -->
<div class="form-group">
<label for="years" class="col-xs-5 control-label">Years</label>
<div class="col-xs-2">
<input type="number" ng-model:"newemployee.years" class="form-control" ng-required="true" />
</div>
</div><!-- BUTTON -->
<div class="center">
<button type="submit" class="btn btn-default btn-success">Validate</button> <button class="btn btn-default btn-warning" ng-click="FormCtrl.setShow(1)">Cancel</button>
</div>
</form>
</div>
</body>
此处angularjs代码
(function () {
var app = angular.module('employe', []);
app.controller("FormController", function () {
this.Edit = 1;
this.isShow = function(checkEdit){
return this.Edit == checkEdit;
};
this.setShow = function(setShow){
return this.Edit = setShow ;
};
console.log(this.Edit);
});
app.controller("ListCtrl", ['$scope', function($scope) {
$scope.list = /*employee*/[];
$scope.addEmployee = function(){
$scope.list.push({
name: $scope.newemployee.name,
age : parseInt($scope.newemployee.age),
nickname : $scope.newemployee.nickname,
job : $scope.newemployee.job ,
years : parseInt($scope.newemployee.years),
salarie : $scope.newemployee.yes + $scope.newemployee.no
});
};
}]);
})();
您可能应该如下初始化 ListCtrl
中的 newemployee
对象
app.controller("ListCtrl", ['$scope', function($scope) {
$scope.list = /*employee*/ [];
$scope.newemployee = {};
$scope.addEmployee = function() {
$scope.list.push({
name: $scope.newemployee.name,
age: parseInt($scope.newemployee.age),
nickname: $scope.newemployee.nickname,
job: $scope.newemployee.job,
years: parseInt($scope.newemployee.years),
salarie: $scope.newemployee.yes + $scope.newemployee.no
});
};
}]);
示例输入标签
<input type="text" class="form-control" ng-required="true" name="name" ng-model="newemployee.name" />
您必须 link 输入字段到您要设置的属性。您可以像这样使用 ng-model 执行此操作:
<input type="texte" class="form-control" ng-required="true" name="name" ng-model="$scope.newemployee.name" />
需要一些关于 angular 的帮助,我对此一无所知,但我有工作要做 ^^'。我的问题是当我写入我的输入时,我无法获得 ng-model return 并得到一个范围错误告诉 $scope.newemployee is undefined
(更正)。
新问题我的 table 'List' 像我的对象 employee
<!DOCTYPE html>
<html ng-app="employe">
<head>
<meta charset="utf-8" />
<title>
List of employe
</title>
<meta name="viewport" content="with=device-width , initial-scale=0 ,shrink- to-fit=no" />
<link rel="stylesheet" href="./files/bootstrap.min.css" type="text/css" />
<link rel="stylesheet" href="./files/style.css" type="text/css" />
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js" type="text/javascript">
</script>
<script type="text/javascript" src="./files/code.js">
</script>
</head>
<body>
<div ng-controller="FormController as FormCtrl">
<button ng-click="FormCtrl.setShow(2)">Add new employee</button>
<form class="form-horizontal" ng-controller="ListCtrl" name="infoForm" ng-submit="addEmployee()" ng-show="FormCtrl.isShow(2)" novalidate="" id="infoForm">
{{newemployee}}
<h2>
Add/Edit employee
</h2><!-- NOM -->
<div class="form-group">
<label for="name" class="col-xs-5 control-label">Name</label>
<div class="col-xs-2">
<input type="texte" ng-model:"newemployee.name" class="form-control" ng-required="true" name="name" />
</div>
</div><!-- AGE -->
<div class="form-group">
<label for="age" class="col-xs-5 control-label">Age</label>
<div class="col-xs-2">
<input type="number" ng-model:"newemployee.age" class="form-control" ng-required="true" />
</div>
</div><!-- NICKNAME -->
<div class="form-group">
<label for="nickname" class="col-xs-5 control-label">Nickname</label>
<div class="col-xs-2">
<input type="texte" ng-model:"newemployee.nickname" class="form-control" ng-required="true" />
</div>
</div><!-- EMPLOYEE -->
<div class="form-group">
<label for="employee" class="col-xs-5 control-label">Employee</label>
<div class="col-xs-2">
<label class="radio-inline"><input type="radio" name="inlineRadioOptions" id="Yes" ng-model:"newemployee.yes" value="yes" />Yes</label> <label class="radio-inline"><input type="radio" name="inlineRadioOptions" id="No" ng-model:"newemployee.no" value="no" />No</label>
</div>
</div><!-- JOB -->
<div class="form-group">
<label for="job" class="col-xs-5 control-label">Job</label>
<div class="col-xs-2">
<select ng-model:"newemployee.job" class="form-control">
<option>
Founder
</option>
<option>
Market chef
</option>
<option>
Stage
</option>
</select>
</div>
</div><!-- ANNEE -->
<div class="form-group">
<label for="years" class="col-xs-5 control-label">Years</label>
<div class="col-xs-2">
<input type="number" ng-model:"newemployee.years" class="form-control" ng-required="true" />
</div>
</div><!-- BUTTON -->
<div class="center">
<button type="submit" class="btn btn-default btn-success">Validate</button> <button class="btn btn-default btn-warning" ng-click="FormCtrl.setShow(1)">Cancel</button>
</div>
</form>
</div>
</body>
此处angularjs代码
(function () {
var app = angular.module('employe', []);
app.controller("FormController", function () {
this.Edit = 1;
this.isShow = function(checkEdit){
return this.Edit == checkEdit;
};
this.setShow = function(setShow){
return this.Edit = setShow ;
};
console.log(this.Edit);
});
app.controller("ListCtrl", ['$scope', function($scope) {
$scope.list = /*employee*/[];
$scope.addEmployee = function(){
$scope.list.push({
name: $scope.newemployee.name,
age : parseInt($scope.newemployee.age),
nickname : $scope.newemployee.nickname,
job : $scope.newemployee.job ,
years : parseInt($scope.newemployee.years),
salarie : $scope.newemployee.yes + $scope.newemployee.no
});
};
}]);
})();
您可能应该如下初始化 ListCtrl
中的 newemployee
对象
app.controller("ListCtrl", ['$scope', function($scope) {
$scope.list = /*employee*/ [];
$scope.newemployee = {};
$scope.addEmployee = function() {
$scope.list.push({
name: $scope.newemployee.name,
age: parseInt($scope.newemployee.age),
nickname: $scope.newemployee.nickname,
job: $scope.newemployee.job,
years: parseInt($scope.newemployee.years),
salarie: $scope.newemployee.yes + $scope.newemployee.no
});
};
}]);
示例输入标签
<input type="text" class="form-control" ng-required="true" name="name" ng-model="newemployee.name" />
您必须 link 输入字段到您要设置的属性。您可以像这样使用 ng-model 执行此操作:
<input type="texte" class="form-control" ng-required="true" name="name" ng-model="$scope.newemployee.name" />