当谈到angularjs中的声明性语法时,我们通常总是提出指令,以及我们如何将$scope
属性传递给这些指令以进行处理,DOM操作,数据收集以及这些指针的各种组合。 / p>
目前,我努力说服我的团队(更不用说我自己)说声明模板是正确的方法。这并不一定意味着使用指令,而是如何使用指令。
以下控制器/模板对显示了一个示例,其中我根据" model" / $ scope&s属性显示元素。
function MainController($scope) {
$scope.hasJQuery = true;
$scope.hasProblems = true;
$scope.hasLodash = false;
$scope.hasStuff = true;
}
<div ng-controller="MainController">
<span ng-if="hasJquery && hasProblems && hasLodash && hasStuff">TL;DR?</span>
<div>
但另一种方法可能看起来像这样
function MainController2($scope) {
// Get/Retrieve $scope stuff...
// Setup "ViewModel" Data
$scope.hasJQueryAndProblemsAndLodashAndStuff = $scope.hasJQuery && $scope.hasProblems && $scope.hasLodash && $scope.hasStuff;
$scope.hasJQueryAndLodash = $scope.hasJQuery && $scope.hasLodash;
}
<div ng-controller="MainController">
<span ng-if="hasJQueryAndProblemsAndLodashAndStuff">...</span>
<span ng-class="hasJqueryAndLodash ? 'blue' : ''"></span>
</div>
该名称是isReady
或hasAll
之类的夸张版本,但重点是要显示有时html会需要多个ng-class
属性,其中各种逻辑会依赖$scope
属性。
我大多看到第一个例子是人们通常会使用的 - 除非逻辑变得非常复杂。另外我也知道angularjs是一个MVW,但这对我没有帮助。
我应该只添加&#34; ViewModel&#34; (控制器中的$scope
的(表示逻辑)有时会?每时每刻?决不?将表示逻辑放入控制器/模板的优点/缺点是什么?