我有一个能储存超级英雄的控制器。我想使用一个自定义指令来显示基于输入字段中输入的值的某个超级英雄。我不希望用过滤器来实现这一点。我现在被困住了,我想知道我是否需要实现类似$watch这样的东西来完成这个任务?我真的很想用最好的方法来做这件事。提前感谢!
heroController.js
var myApp = angular.module('myApp',[]);
myApp.controller('heroController', ['$scope', function($scope) {
$scope.superHero = [
{firstName: 'superman', image: 'img/url.com', superPower: 'invisibility'},
{firstName: 'batman', image: 'img/url.com', superPower: 'xray'},
{firstName: 'stretch', image: 'img/url.com', superPower: 'flight'},
{firstName: 'speedy', image: 'img/url.com', superPower: 'strength'},
{firstName: 'aquaman', image: 'img/url.com', superPower: 'aqua'},
{firstName: 'hulk', image: 'img/url.com', superPower: 'stretch'}
];
}]);heroDirective.js
myApp.directive('heroDirective', function(){
return {
restrict: 'EA',
templateUrl: 'heroView.html',
replace: true,
scope: {
firstName: '@fname',
lastName: '@lname',
superPower: '@spower'
}
};
});heroView.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<figure>
<ul>
<li>{{superHero.firstName}}</li>
<li>{{superHero.lastName}}</li>
<li>{{superHero.image}}</li>
<li>{{superHero.superPower}}</li>
</ul>
</figure>
</body>
</html>index.html
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<body ng-app="myApp">
<div ng-controller="heroController">
<input ng-model="heroDirective.firstName"/>
<hero-directive ></hero-directive>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.js"></script>
<script src="heroController.js"></script>
<script src="heroDirective.js"></script>
</body>发布于 2014-10-02 19:20:39
对于filter来说,这是一个很好的例子,但是由于您要求不使用它,所以我将以角的方式实现它:
http://plnkr.co/edit/rsStQ5lKdG9Qvm6X4Udz?p=preview
.html
<span>insert hero:</span>
<input ng-model="superHeroInput" />
<hero-directive super-hero="selectedSuperHero"></hero-directive>.js
var app = angular.module('app', []);
app.controller('heroController', function($scope, $location, $timeout) {
$scope.superHeros = [{
firstName: 'superman',
image: 'img/url.com',
superPower: 'invisibility'
}, {
firstName: 'batman',
image: 'img/url.com',
superPower: 'xray'
}, {
firstName: 'stretch',
image: 'img/url.com',
superPower: 'flight'
}, {
firstName: 'speedy',
image: 'img/url.com',
superPower: 'strength'
}, {
firstName: 'aquaman',
image: 'img/url.com',
superPower: 'aqua'
}, {
firstName: 'hulk',
image: 'img/url.com',
superPower: 'stretch'
}];
$scope.superHeroInput = '';
$scope.$watch('superHeroInput', function(newValue) {
if (!newValue || newValue.length == 0) {
$scope.selectedSuperHero = $scope.superHeros[0];
} else {
var randomItem = $scope.superHeros[Math.floor(Math.random() * $scope.superHeros.length)];
$scope.selectedSuperHero = randomItem;
}
});
});
app.directive('heroDirective', function() {
return {
restrict: 'EA',
templateUrl: 'heroView.html',
replace: true,
scope: {
superHero: '='
}
};
});heroView.html
<figure>
<ul>
<li>{{superHero.firstName}}</li>
<li>{{superHero.image}}</li>
<li>{{superHero.superPower}}</li>
</ul>
</figure>那发生什么事了?以superHeroInput为界的输入文本。当它发生变化时,使用新的输入调用$scope.$watch('superHeroInput', ...,然后如果有空字符串,我选择了第一个超级英雄,否则,随机超级英雄(您应该将超级英雄过滤逻辑放在那里)。
在此之后,所选的超级英雄和指令之间有一个绑定。当选定的超级英雄被更改时,指令将更新他的视图。
https://stackoverflow.com/questions/26167539
复制相似问题