[英]Switch view in view in AngularJS upon click
Hello dear Stackoverflow. 您好,亲爱的Stackoverflow。
I am currently developing a AnguarJS project, and I have made some headway. 我目前正在开发AnguarJS项目,并且取得了一些进展。
However, now I want to change a viewbox another view, on click, and I have difficulty figuring out how to go about this. 但是,现在我想在单击时将视图更改为另一个视图,并且我很难弄清楚该怎么做。
So the controller has a factory which in turn have some data (names and age). 因此,控制器有一个工厂,而工厂又有一些数据(名称和年龄)。 The names get loaded into a select with ng-options, and depending on the name, a viewbox will change to display that name and age.
名称将通过ng-options加载到select中,并且视名称而定,视图框将更改为显示该名称和年龄。
Now I want make a button, which will call a view, which makes it possible to change selected item (I know I need a backend for these actual changes to stick, but for now, I just want it to change). 现在,我想创建一个按钮,该按钮将调用一个视图,从而可以更改选定的项目(我知道需要后端来粘贴这些实际更改,但现在,我只希望它进行更改)。
How should I go about this. 我应该怎么做。 (This code is not refactored, but bear with me :) )
(此代码未重构,请耐心等待:))
views/home.html: views / home.html:
<div class="container">
<div clas="row">
<div style="width: 200px; margin-top: 100px">
<select ng-model="selectedItem" ng-options="patients.name for patients in patients" class="pull-left form-control" name="Vælg"></select>
</div>
<div class="viewbox pull-right">
<h3>Patient: {{selectedItem.name}}</h3>
<p>Age: {{selectedItem.age}} </p>
</div>
</div>
<div class="row" class="pull-left">
<div style="width: 200px; margin-top: 100px">
<form role="form" class="form-group" ng-submit="addPost()">
<input class="form-control" type="text" ng-model="title" />
<input class="form-control" type="text" ng-model="age" />
<button type="submit">Add</button>
</form>
</form>
</div>
</div>
</div>
app.js: app.js:
var app = angular.module('KOL', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/home',
templateUrl: './views/home.html',
controller: 'kolCtrl'
});
$urlRouterProvider.otherwise('home');
}])
.factory('patients', [function(){
var object = {
patients: [
{name: 'Erik', age: 42},
{name: 'Lars', age: 24},
{name: 'Karl', age: 12},
{name: 'John', age: 16}
]
};
return object;
}])
.controller('kolCtrl', ['$scope', 'patients',
function($scope, patients){
$scope.patients = patients.patients;
$scope.selectedItem = $scope.patients[0];
$scope.addPost = function() {
if(!$scope.title || $scope.title === '') { return; }
if(!$scope.age || $scope.age === '') { return; }
$scope.patients.push({name: $scope.title, age: $scope.age});
$scope.title = '';
$scope.age = '';
};
}])
;
If you want to add set the selectedItem
to the newly added element just do this in the addPost
function: 如果要添加,请将
selectedItem
设置为新添加的元素,只需在addPost
函数中执行以下操作:
var newPatient = {name: $scope.title, age: $scope.age};
$scope.patients.push(newPatient);
$scope.selectedItem = newPatient;
Then the new element will be displayed inside the viewbox
element. 然后,新元素将显示在
viewbox
元素内。
Update: 更新:
The original problem was, that ng-options
needed to be adapted to be able to work with the index of the patients
array. 最初的问题是,必须对
ng-options
进行调整以使其能够与patients
阵列的索引配合使用。 The easiest solution was to do the ng-select
differently: 最简单的解决方案是不同地进行
ng-select
:
<select ng-model="selectedItem" ng-options="patients.indexOf(patient) as patient.name for patient in patients"></select>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.