我想更新对象数组中的对象.是否有另一种可能性,而不是迭代所有项目并更新匹配的项目?当前代码如下所示:
angular.module('app').controller('MyController', function($scope) {
$scope.object = {
name: 'test',
objects: [
{id: 1, name: 'test1'},
{id: 2, name: 'test2'}
]
};
$scope.update = function(id, data) {
var objects = $scope.object.objects;
for (var i = 0; i < objects.length; i++) {
if (objects[i].id === id) {
objects[i] = data;
break;
}
}
}
});
Run Code Online (Sandbox Code Playgroud) 我有以下指令用于显示弹出窗口以确认单击时执行函数.
现在我想在我的控制器中使用它来显示一个弹出窗口,如果一个对象的属性已被更改,并且用户想要更改位置而不保存之前的对象.那可能吗?
angular.module('app.confirm', [
'ui.bootstrap',
'template/modal/confirm.html',
])
.controller('ConfirmModalController', ['$scope', '$modalInstance', function($scope, $modalInstance) {
$scope.confirm = function() {
$modalInstance.close();
};
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
}])
.directive('confirm', ['$modal', function($modal) {
return {
restrict: 'A',
scope: {
confirm: '&',
title: '@confirmTitle',
message: '@confirmMessage',
confirmButtonText: '@confirmButtonText',
cancelButtonText: '@cancelButtonText'
},
link: function(scope, element, attributes) {
element.bind('click', function() {
var modal= $modal.open({
controller: 'ConfirmModalController',
templateUrl: 'template/modal/confirm.html',
size: 'sm',
scope: scope
});
modal.result.then(function() {
scope.confirm();
}, function() {
// Modal dismissed
});
});
}
}; …
Run Code Online (Sandbox Code Playgroud) 如何收听特定控制器的调度事件?目前我做了以下事情:
Module.php
public function onBootstrap(EventInterface $event) {
$application = $event->getApplication();
$eventManager = $application->getEventManager();
$serviceManager = $application->getServiceManager();
$eventManager->attach($serviceManager->get('MyListener'));
}
Run Code Online (Sandbox Code Playgroud)
MyListener.php
class MyListener extends AbstractListenerAggregate {
public function attach(EventManagerInterface $eventManager) {
$this->listeners[] = $eventManager->attach(
MvcEvent::EVENT_DISPATCH, function($event) {
$this->setLayout($event);
}, 100
);
}
public function setLayout(EventInterface $event) {
$event->getViewModel()->setTemplate('mylayout');
}
}
Run Code Online (Sandbox Code Playgroud)
这将设置所有控制器调度的布局.现在我只想在应用程序调度特定控制器时设置布局.
我有一个实体“公司”,与其营业时间有一对多关联。开放时间实体有两个时间类型的字段(开始和结束)。现在我想编写一个公司方法“isCurrentlyOpen”,它返回一个布尔值,但我无法得到它......
公司
class Company {
/**
* @var \Gastronome\Entity\OpeningHours[]
*
* @ORM\OneToMany(targetEntity="Gastronome\Entity\OpeningHours", mappedBy="gastronome", fetch="EAGER")
* @ORM\OrderBy({"weekday" = "ASC", "start" = "ASC"})
*/
private $openingHours;
public function getOpeningHours() {
return $this->openingHours;
}
public function getTodaysOpeningHours() {
$criteria = Criteria::create()
->where(Criteria::expr()->eq('weekday', (int) date('w')));
return $this->getOpeningHours()->matching($criteria);
}
public function isCurrentlyOpen() {
$criteria = Criteria::create()
->where(Criteria::expr()->gt('start', new \DateTime()));
->andWhere(Criteria::expr()->lt('end', new \DateTime()));
return count($this->getTodaysOpeningHours()->matching($criteria)) > 0;
}
}
Run Code Online (Sandbox Code Playgroud)
营业时间
class OpeningHours {
/**
* @ORM\Column(type="time", nullable=false)
*/
private $start;
/**
* @ORM\Column(type="time", nullable=false)
*/
private …
Run Code Online (Sandbox Code Playgroud)