我想知道是否有办法将参数传递给指令?
我想要做的是从控制器附加一个指令,如下所示:
$scope.title = "title";
$scope.title2 = "title2";
angular.element(document.getElementById('wrapper')).append('<directive_name></directive_name>');
Run Code Online (Sandbox Code Playgroud)
是否可以同时传递参数,以便我的指令模板的内容可以链接到一个或另一个范围?
这是指令:
app.directive("directive_name", function(){
return {
restrict:'E',
transclude:true,
template:'<div class="title"><h2>{{title}}</h3></div>',
replace:true
};
})
Run Code Online (Sandbox Code Playgroud)
如果我想使用相同的指令但使用$ scope.title2会怎么样?
我在启动时修改UIView高度时遇到问题.
我必须UIView,我想要一个屏幕尺寸*70和另一个填补空白.
这就是我所拥有的
@IBOutlet weak var questionFrame: UIView!
@IBOutlet weak var answerFrame: UIView!
let screenSize:CGRect = UIScreen.mainScreen().bounds
Run Code Online (Sandbox Code Playgroud)
和
questionFrame.frame.size.height = screenSize.height * 0.70
answerFrame.frame.size.height = screenSize.height * 0.30
Run Code Online (Sandbox Code Playgroud)
它在运行时对应用程序没有影响.我使用自动布局,但我只有边距约束...
我做错了吗?
我试图创建一个集合视图,单元格显示可变长度的字符串.
我使用此功能来设置单元格布局:
func collectionView(collectionView : UICollectionView,layout collectionViewLayout:UICollectionViewLayout,sizeForItemAtIndexPath indexPath:NSIndexPath) -> CGSize
{
var cellSize:CGSize = CGSizeMake(self.whyCollectionView.frame.width, 86)
return cellSize
}
Run Code Online (Sandbox Code Playgroud)
我想做的是cellSize.height根据我的cell.labelString.utf16Count长度操纵.基本的逻辑就是那个
if((cell.labelString.text) > 70){
cellSize.height = x
}
else{
cellSize.height = y
}
Run Code Online (Sandbox Code Playgroud)
但是,我无法设法检索始终返回nil的单元格标签字符串长度.(我认为它尚未加载......
为了更好地理解,这里是完整的代码:
// WhyCell section
var whyData:NSMutableArray! = NSMutableArray()
var textLength:Int!
@IBOutlet weak var whyCollectionView: UICollectionView!
//Loading data
@IBAction func loadData() {
whyData.removeAllObjects()
var findWhyData:PFQuery = PFQuery(className: "PlacesWhy")
findWhyData.whereKey("placeName", equalTo: placeName)
findWhyData.findObjectsInBackgroundWithBlock({
(objects:[AnyObject]!,error:NSError!)->Void in
if (error == nil) {
for object in objects { …Run Code Online (Sandbox Code Playgroud) 尝试使用以太坊来解决区块链问题,在尝试与已部署的合同进行交互时,我遇到了问题.我想要实现的是调用一种方法来显示添加到使用Geth本地部署的私有区块链的信息.
我无法通过我的智能合约调用任何功能,我一直在想我是否做错了什么......有人能告诉我如何从这个合同中简单地调用其中一个方法吗?让我们说出显示现有的代理商,或者用户所属的代理商名称.
我的合同:agency.sol
pragma solidity ^0.4.18;
// We have to specify what version of compiler this code will compile with
contract Agency {
event NewAgency(uint agencyId, string name, uint dna);
uint dnaDigits = 16;
uint dnaModulus = 10 ** dnaDigits;
//agency structure
struct Agency {
string name;
uint dna;
}
Agency[] public agencies;
mapping (uint => address) public agencyToOwner;
mapping (address => uint) ownerAgencyCount;
function _createAgency(string _name, uint _dna) private {
uint id = agencies.push(Agency(_name, _dna)) - 1;
agencyToOwner[id] …Run Code Online (Sandbox Code Playgroud) 我试图迭代我从一个内部的多个http调用构建的数组 angular.forEach()
功能
$scope.ticket_stats = function(){
//cleaning variables
$scope.data_set = [];
$scope.closed_tickets = [];
//fetching time stamps (epoch)
$scope.time_frame = time_period.days(7);
//calling data using time stamps
angular.forEach($scope.time_frame, function(item) {
//debug
console.log(item);
var promise = tickets.status("closed", item);
promise.success(function(data){
console.log(data);
$scope.closed_tickets.push(data[0].datapoints[0][0]); // returns a numerical value
});
});
//SEE MESSAGE BELOW
$scope.data_set.push($scope.closed_tickets);
}
Run Code Online (Sandbox Code Playgroud)
最后一行$scope.data_set.push()正在工作但是一旦调用返回数据就会随着时间的推移自行增 我想在for Each循环中的所有内容都完成之后执行此行.我需要迭代$scope.closed_tickets数组,然后在其中播放(添加)数据并构建第二个数组.
以下是此功能中使用的服务:
// CALL TICKETS STATS
app.service('tickets', function($http){
this.status = function(status, date){
var one_snap = date - 100;
var url = "/url/render?format=json&target=sum(stats.tickets."+status+")&from="+one_snap+"&until="+date+"";
return …Run Code Online (Sandbox Code Playgroud) 编辑:
添加异常断点(感谢Daniel T)后,我发现这是我的
cell.askingUsernameLabel.text = NSString(string: arr[indexPath.row])
Run Code Online (Sandbox Code Playgroud)
返回零。
解决此问题的方法是,迅速注册子类将覆盖您在情节提要中所做的事情(如果使用情节提要)。只需删除
self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell")
Run Code Online (Sandbox Code Playgroud)
而且你很好走
原版的
我真的迷失了这一点:我试图在我的ViewController的UIView中设置一个UICollectionView。(故事板)
我通过几个教程/代码示例进行了设置,但是我无法找出为什么最终总是以“致命错误:在解开Optional值时意外发现nil”而结束。
这是代码。
控制器层次结构:
View Controller (class:ViewController.swift)
-- UIView 1
-- UIView 2
----- UICollectionView
---------- UICollectionViewCell (class:CollectionViewCell.swift) (dataSource & delegate linked to View Contorller)
--------------- Label
Run Code Online (Sandbox Code Playgroud)
ViewController.swift
class ViewController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
var arr:[String] = []
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
arr = ["title1", "title2"]
self.collectionView.registerClass(CollectionViewCell.self, forCellWithReuseIdentifier:"CollectionViewCell")
}
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, …Run Code Online (Sandbox Code Playgroud) 我有一组集合,我想检查两个或多个集合是否相同。我不需要按照精确的顺序查看这些集合。
示例列表
list = [
{7, 12, 16, 17, 31},
{33, 4, 8, 10, 46},
{6, 40, 43, 22, 29},
{2, 35, 9, 41, 31},
{34, 38, 42, 43, 45},
{38, 16, 20, 25, 30},
{2, 10, 45, 19, 25},
{4, 44, 41, 14, 16},
{39, 40, 16, 25, 28},
{34, 37, 45, 19, 23},
{4, 41, 44, 14, 16},
]
Run Code Online (Sandbox Code Playgroud)
列表中有两个相同的集合,但编号顺序不同。如何检查这些?
我没有找到任何好的例子来使用swift做我想做的事情所以我允许自己提出这个问题.
我使用collectionView来显示PFObjects,我想使用prepareForSegue将显示的单元格数据发送到第二个控制器.
在这一点上,我努力使这部分代码工作:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "goto_answerquestion"){
var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()
}
}
Run Code Online (Sandbox Code Playgroud)
这一行:
var indexpath : NSIndexPath = self.collectionView.indexPathsForSelectedItems()
Run Code Online (Sandbox Code Playgroud)
触发以下错误:
(UICollectionView, numberOfItemsInSection: Int)-> Int does not have a member named 'indexPathsForSelectedItems'
Run Code Online (Sandbox Code Playgroud)
如果我使用了错误的方法,或者如果您需要其他数据来对问题进行适当的概述,请告诉我.
回答
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if(segue.identifier == "segue_identifier"){
// check for / catch all visible cell(s)
for item in self.collectionView!.visibleCells() as [UICollectionViewCell] {
var indexpath : NSIndexPath = self.collectionView.indexPathForCell(item as CollectionViewCell)!
var cell : CollectionViewCell = …Run Code Online (Sandbox Code Playgroud) 我正在使用文件夹/文件应用程序,用户可以在其中将文件下载到本地磁盘。每当用户下载文件时,我都想显示一个显示进度的下载栏。
为此,我创建了一个协议,该协议允许我的下载类和视图控制器进行通信:
协议:
protocol DownloadResponder : class {
func downloadFinished()
func downloadProgress(current:Int64, total:Int64)
}
Run Code Online (Sandbox Code Playgroud)
下载类:
class fileDownloader: NSObject, NSURLSessionDelegate, NSURLSessionDownloadDelegate {
//responder
var responder : MyAwesomeDownloadResponder?
init(responder : MyAwesomeDownloadResponder) {
self.responder = responder
}
...
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("downloaded \(100*totalBytesWritten/totalBytesExpectedToWrite)")
responder?.downloadProgress(totalBytesWritten, total: totalBytesExpectedToWrite)
}
...
}
Run Code Online (Sandbox Code Playgroud)
然后在我的视图控制器中,我有一个触发downloadProgress功能的下载按钮:
func downloadProgress(current:Int64, total:Int64) {
let priority = DISPATCH_QUEUE_PRIORITY_DEFAULT
dispatch_async(dispatch_get_global_queue(priority, 0)) {
// do some task
var currentProgress = 100 * current …Run Code Online (Sandbox Code Playgroud) 我对 shell 脚本真的很陌生,我想检查软件版本号并为其设置条件。
\n\n例如:检查 python 版本\xc2\xa0> 2.7.0\n然后\n...
\n\n我可以使用以下命令检查 python:
\n\nif [ "$(python -V 2>&1)" ]\nthen\n pyv="$(python -V 2>&1)"\n echo "$pyv"\nfi\nRun Code Online (Sandbox Code Playgroud)\n 我有一个带有按钮的viewController,允许用户从URL下载文件.为了保持我的代码清洁,我创建了一个从我的视图中调用的下载类.它工作正常但现在我想为用户提供有关下载的一些UI提示.
假设我有一个标签,我想在下载结束后将其文本更改为"已下载".应该怎么做?
这是我的代码:
FileViewController
@IBOutlet weak var downloadLbl: UILabel!
func downloadFile(sender:UIButton!)
{
fileDownloader().download_zip(datastring, destination: path, name: naming, fileis: self.fileId)
}
Run Code Online (Sandbox Code Playgroud)
(这只是重要的部分)
然后我将我的下载功能放在一个单独的类文件中,然后启动一个DownloadTaskSession并使用以下委托来观察下载完成
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
println("session \(session) has finished the download task \(downloadTask) of URL \(location).")
FileViewController().downloadLbl.text = "downloaded"
}
Run Code Online (Sandbox Code Playgroud)
最后一行:FileViewController().downloadLbl.text = "downloaded"返回错误:致命错误:在展开Optional值时意外发现nil.
有人可以帮忙吗?
结论
这两个提议的解决方案在创建通信之间的通信时都有用,但是我去了通知解决方案,因为在我的情况下我必须更新UI以及后台线程进展并且使用协议不能按预期工作.进行了通信,但即使在使用dispatch_async方法推回主线程上的更改之后,更新UI仍无法正常工作.
使用通知系统有帮助,因为它更容易实现,管理和用于UI修改.