我正在使用下面這段代碼作爲指令,而我正在使用此代碼ng-model在控制器中返回空值。在控制器中返回ng-model值爲空的指令
myApp.directive('tabs', function() {
return {
restrict: 'E',
transclude: true,
scope: {},
controller: [ "$scope", function($scope) {
var panes = $scope.panes = [];
$scope.select = function(pane) {
angular.forEach(panes, function(pane) {
pane.selected = false;
});
pane.selected = true;
};
this.addPane = function(pane) {
if (panes.length === 0) $scope.select(pane);
panes.push(pane);
};
}],
template:
'<div class="tabbable">' +
'<ul class="nav nav-tabs">' +
'<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">'+
'<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
'</li>' +
'</ul>' +
'<div class="tab-content" ng-transclude></div>' +
'</div>',
replace: true
};
}).
directive('pane', function() {
return {
require: '^tabs',
restrict: 'E',
transclude: true,
scope: { title: '@' },
link: function(scope, element, attrs, tabsCtrl) {
tabsCtrl.addPane(scope);
},
template:
'<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
'</div>',
replace: true
};
});
下的選項卡我有兩個文本字段
<form role="form">
<legend>Add City</legend>
<div class="form-group">
<label for="">State</label>
<input type="text" ng-model="location.state" id="input" class="form-control" placeholder="Enter state name">
</div>
<div class="form-group">
<label for="">City</label>
<input type="text" ng-model="location.city" id="input" class="form-control" placeholder="Enter city name">
</div>
<button type="button" class="btn btn-success">Create</button>
<button type="button" ng-click="UpdateCity()" class="btn btn-warning">Update</button>
</form>
,如果我嘗試獲得$scope.location.city
和控制器$scope.location.state
價值正在逐漸空值,但如果我在視圖檢查{{location.city}}
我得到的價值。
之前使用tab指令形式工作完美,使用tab指令後得到此錯誤。
您是否在視圖中定義了「位置」對象? – Grundy
爲什麼我應該定義位置 – Nodemon